173k views
4 votes
write a program that calculates the occupancy rate for a hotel. the program should start by asking the user how many floors the hotel has. a loop should then iterate once for each floor. in 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 how many rooms the hotel has, how many of them are occupied, how many are unoccupied, and the percentage of rooms that are occupied. the percentage may be calculated by dividing the number of rooms occupied by the number of rooms

User Tanerkay
by
8.1k points

1 Answer

5 votes

Answer:

# Get the number of floors from the user

floors = int(input("How many floors does the hotel have? "))

# Initialize variables to keep track of the number of rooms and occupied rooms

total_rooms = 0

total_occupied_rooms = 0

# Loop once for each floor

for floor in range(floors):

# Get the number of rooms and occupied rooms on this floor

rooms = int(input("How many rooms are on floor " + str(floor + 1) + "? "))

occupied_rooms = int(input("How many of these rooms are occupied? "))

# Update the total number of rooms and occupied rooms

total_rooms += rooms

total_occupied_rooms += occupied_rooms

# Calculate the percentage of rooms that are occupied

occupancy_rate = (total_occupied_rooms / total_rooms) * 100

# Display the results

print("The hotel has a total of", total_rooms, "rooms.")

print("There are", total_occupied_rooms, "occupied rooms.")

print("There are", total_rooms - total_occupied_rooms, "unoccupied rooms.")

print("The occupancy rate is", occupancy_rate, "%.")

Step-by-step explanation:

This program uses a for loop to iterate once for each floor. On each iteration, it gets the number of rooms and occupied rooms on the floor from the user and updates the total number of rooms and occupied rooms accordingly. Finally, it calculates the occupancy rate and displays the results.

User Minh Bui
by
7.3k points