107k views
1 vote
Write a program that calculates the occupancy rate for each floor of a hotel. The program should start by asking for the number of floors the hotel has. A loop should then iterate once for each floor. During each iteration, the loop should ask the user for the number of rooms on the floor, and how many of them are occupied. After all the iterations, the program should display the number of rooms the hotel has, the number that are occupied, the number that are vacant, and the occupancy rate for the hotel. Input Validation: Do not accept a value less than 1 for the number of floors. Do not accept a number less than 10 for the number of rooms on a floor.

1 Answer

4 votes

Answer:

# User is prompted to enter the number of floor in hotel

# The received value is assigned to no_of_floor

no_of_floor = int(input("Enter the number of floor in the hotel."))

# This loop is to enforce that user input is not less than 1

while (no_of_floor < 1):

no_of_floor = int(input("Enter the number of floor in the hotel."))

# counter variable is initialized to loop through the no_of_floor

counter = 1

# total number of rooms occupied in the hotel is initialized to 0

total_occupied = 0

# total number of rooms vacant in the hotel is initialized to 0

total_vacant = 0

# total number of rooms in the hotel is initialized to 0

total_room = 0

# loop through each floor

while counter <= no_of_floor:

# number of room in a floor is received from user

number_of_room = int(input("Enter the number of room in floor: "))

# this loop ensure that the number must not be less than 10

while (number_of_room < 10):

number_of_room = int(input("Enter the number of room in floor "))

# number of occupied room is a floor is accepted from user

number_of_occupied = int(input("Enter the number of occupied room."))

# this loop ensure that the no_of_occupied is less than no_of_room

while (number_of_occupied > number_of_room):

number_of_occupied = int(input("Enter the number of occupied room."))

# number of vacant room in a floor is calculated

floor_vacant = number_of_room - number_of_occupied

# total number of occupied room is calculated

total_occupied += number_of_occupied

# total room in the hotel is calculated

total_room += number_of_room

# total number of vacant room is calculated

total_vacant += floor_vacant

# the counter is increment to move to the next floor

counter += 1

# occupancy_rate is calculated as a percentage

occupancy_rate = (total_occupied / total_room) * 100

# Number of total room is displayed

print("The total number of room in the hotel is: ", total_room)

# Number of total vacant room is displayed

print("The number of vacant room in the hotel is: ", total_vacant)

# Number of total occupied room is displayed

print("The total number of occupied room in the hotel is: ", total_occupied)

# The occupancy rate for the hotel is displayed to 2 decimal

# place percent

print("The occupancy rate for the hotel is: {:.2f}%".format(occupancy_rate))

Step-by-step explanation:

The program is well commented. It put all the constraint into consideration like:

  • not allowing a user to enter less than 1 for number of floors
  • not allowing a user to enter less than 10 for number of rooms in a floor
  • not allowing a user to enter number of occupied room greater than number of room in a floor.
User Rplusg
by
5.4k points