I looked out various articles but, couldn't find a way. So, posting here in hope of finding a logic for this particular python problem.
I am working on a hotel management system's book a room section. Here, the first system asks the user how many total guests then asks the total required rooms. The hotel has the condition that one room allows up to 4 guests. And automatically auto-suggests a number of rooms should be taken to the guest if the guest entered the wrong count.
Sample output:
5
1
You cannot take 1 room. You shall take 2 rooms for 5 guests. Per room, `
only 4 guests are allowed.`
Sample Output 2:
9
1
You cannot take 1 room. Yu shall take 3 rooms for 9 guests. Per room,
only 4 guests are allowed.
enter code here
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.
I am not able to figure out a way to show the room occupancy thing. I tried modulo but for the multiples of 4, it is giving the wrong rooms count suggestion.
I tried these,
total_guests%4 (total guests-per_room_capacity)% rooms required by the guest
Can anyone suggest a way to do this? I am stuck at this!
CodePudding user response:
You can utilize the math.ceil()
function for this.
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))
CodePudding user response:
Here's one way to do what you are asking.
Essentially, you can perform integer division using the //
operator. This will round down to one less room than is needed except for the case where guests
is a multiple of 4, in which case it gives the desired result. So, if the number of guests
is not a multiple of 4, we need to add 1. We can detect whether guests
is a multiple of 4 using the modulo operator %
, as shown below.
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'}.")
Here is the sample output:
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.