gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
write a hotel reservation program in python and explain to me
- User
write a hotel reservation program in python and explain to me
- Assistant
write a hotel reservation program in python and explain to me
Sure! Here's a simple hotel reservation program in Python:
rooms = {
'101': {'room_type': 'Single', 'price': 50, 'occupancy': 1},
'102': {'room_type': 'Double', 'price': 75, 'occupancy': 2},
'103': {'room_type': 'Suite', 'price': 100, 'occupancy': 4},
}
def display_available_rooms():
print("Available Rooms:")
for room_num, room_details in rooms.items():
if room_details['occupancy'] > 0:
print(f"Room Number: {room_num}, Type: {room_details['room_type']}, Price: {room_details['price']}")
def reserve_room(room_num, num_guests):
if room_num in rooms:
if rooms[room_num]['occupancy'] >= num_guests:
print("Reservation successful!")
rooms[room_num]['occupancy'] -= num_guests
return
else:
print("Not enough occupancy for the specified room.")
else:
print("Invalid room number.")
def cancel_reservation(room_num, num_guests):
if room_num in rooms:
rooms[room_num]['occupancy'] += num_guests
print("Reservation cancelled successfully.")
else:
print("Invalid room number.")
def main():
while True:
print("\n1. Display Available Rooms")
print("2. Reserve a Room")
print("3. Cancel Reservation")
print("4. Exit")
choice = input("Enter your choice (1-4): ")
if choice == '1':
display_available_rooms()
elif choice == '2':
room_num = input("Enter the room number: ")
num_guests = int(input("Enter the number of guests: "))
reserve_room(room_num, num_guests)
elif choice == '3':
room_num = input("Enter the room number: ")
num_guests = int(input("Enter the number of guests: "))
cancel_reservation(room_num, num_guests)
elif choice == '4':
print("Thanks for using the Hotel Reservation Program!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == '__main__':
main()
Explanation:
rooms, where each room number is a key mapped to a dictionary containing the room type, price, and current occupancy.display_available_rooms(): This function iterates over the rooms dictionary and prints details of the available rooms (rooms with occupancy greater than 0).reserve_room(room_num, num_guests): This function checks if the specified room exists and has enough occupancy for the number of guests. If the conditions are met, it updates the occupancy and confirms the reservation.cancel_reservation(room_num, num_guests): This function checks if the specified room exists and increments the occupancy by the specified number of guests, effectively canceling the reservation.main() function runs in a loop, displaying a menu and accepting user input to choose an action.To test the program, you can run it and interact with the menu options to display available rooms, reserve a room, cancel a reservation, or exit the program.