需要根据酒店的每间客房容量计算酒店客人的房间分配

Need to calculate Room allotment to the guest in a hotel according to the per room capacity in a hotel

我看了各种文章,但找不到方法。所以,在这里发帖希望能找到解决这个特定 python 问题的逻辑。

我正在开发酒店管理系统的预订房间部分。在这里,第一个系统询问用户总共有多少客人,然后询问所需房间总数。 酒店的条件是一间客房最多可入住4位客人。如果客人输入了错误的计数,它会自动为客人建议应该带走的房间数量。

示例输出:

5
1

You cannot take 1 room. You shall take 2 rooms for 5 guests. Per room,  
only 4 guests are allowed.

示例输出 2:

9
1
You cannot take 1 room. You shall take 3 rooms for 9 guests. Per room, 
only 4 guests are allowed.

room=[]
per_room = 4 # 4 guests in one room
guests = int(input("Guest(s) Count: ")) # takes total guests
total_rooms = int(input("Rooms Count: ")) # takes total room required by the guest

capacity = round(guests % per_room) # condition for according to one room occupancy 
if capacity != total_rooms:
    # you cannot take 1 room for 5 guests. 

我想不出一种方法来显示房间入住情况。我试过取模,但对于 4 的倍数,它给出了错误的房间计数建议。

我试过这些,

total_guests%4 (总计guests-per_room_capacity)客人需要的房间数

任何人都可以建议一种方法吗?我卡在这了!

您可以为此使用 math.ceil() 函数。

import math

room = []
per_room = 4 # 4 guests in one room
guests = int(input("Guest(s) Count: ")) # takes total guests
total_rooms = int(input("Rooms Count: ")) # takes total room required by the guest

if total_rooms * per_room < guests: # Run if the guests can't fit in the rooms
    suggested_rooms = math.ceil(guests / per_room) # Find the amount of rooms needed to fit the guests
    print("You cannot take {} room(s). You shall take {} room(s) for {} guest(s). Per room, only {} guest(s) is/are allowed.".format(total_rooms, suggested_rooms, guests, per_room))

这是完成您所要求的一种方法。

本质上,您可以使用 // 运算符执行整数除法。除了 guests 是 4 的倍数的情况外,这将向下舍入到比所需空间少一个空间,在这种情况下它会给出所需的结果。所以,如果guests的个数不是4的倍数,我们需要加1。我们可以通过取模运算符%检测guests是否是4的倍数,如图下面。

        per_room = 4 # 4 guests in one room
        test_cases = [(5, 1), (9, 1), (5, 2), (9, 2), (9, 3)]
        for guests, total_rooms in test_cases:
            #guests = int(input("Guest(s) Count: ")) # takes total guests
            #total_rooms = int(input("Rooms Count: ")) # takes total room required by the guest

            capacity = guests // per_room + (1 if guests % per_room else 0) # condition for according to one room occupancy 
            if capacity != total_rooms:
                print(f"You cannot take {total_rooms} room{'' if total_rooms == 1 else 's'}."
                      f" You shall take {capacity} room{'' if capacity == 1 else 's'} for "
                      f"{guests} guest{'' if guests == 1 else 's'}."
                      f" Per room, only {per_room} guest{' is' if per_room == 1 else 's are'} allowed.")
            else:
                print(f"Thank you for your reservation of "
                      f"{total_rooms} room{'' if total_rooms == 1 else 's'}"
                      f" for {guests} guest{'' if guests == 1 else 's'}.")

这是示例输出:

You cannot take 1 room. You shall take 2 rooms for 5 guests. Per room, only 4 guests are allowed.`
You cannot take 1 room. You shall take 3 rooms for 9 guests. Per room, only 4 guests are allowed.`
Thank you for your reservation of 2 rooms for 5 guests.
You cannot take 2 rooms. You shall take 3 rooms for 9 guests. Per room, only 4 guests are allowed.`
Thank you for your reservation of 3 rooms for 9 guests.