Bus Seat Reservation Project
Coding Challenge in Python
Write a Python program to book seats in a bus commercial route. The seats map of the bus is as follows:
1 ABC
2 ABC
3 ABC
4 ABC
5 ABC
6 ABC
7 ABC
8 ABC
9 ABC
10 ABC
The program will display the 30 seats available, request the user to enter the seat desired, and then the display available seats updated. Reserved seats will not appear. The program will go on until the bus is full or until the user signals that the program should end.
If the user inputs a seat that is already booked, the program should say that the seat is occupied and ask for another choice.
Hints
- Create a data structure to represent the seats in the bus, such as a list of lists or a dictionary of dictionaries.
- Prompt the user to input the desired seat (row number plus seat letter).
- Check if the entered seat is available or already booked. If it’s already booked, ask the user to choose another seat.
- Display the updated list of available seats after each booking.
- Continue the booking process until all seats are booked or until the user signals the end of the program.
Expected Output
Welcome to the bus seat booking system! Row 1: Available seats - A, B, C Row 2: Available seats - A, B, C Row 3: Available seats - A, B, C Row 4: Available seats - A, B, C Row 5: Available seats - A, B, C Row 6: Available seats - A, B, C Row 7: Available seats - A, B, C Row 8: Available seats - A, B, C Row 9: Available seats - A, B, C Row 10: Available seats - A, B, C Enter the desired seat (row number followed by seat letter, e.g., 3A) or enter 'exit' to end: 5B Seat 5B has been successfully booked. Row 1: Available seats - A, B, C Row 2: Available seats - A, B, C Row 3: Available seats - A, B, C Row 4: Available seats - A, B, C Row 5: Available seats - A, C Row 6: Available seats - A, B, C Row 7: Available seats - A, B, C Row 8: Available seats - A, B, C Row 9: Available seats - A, B, C Row 10: Available seats - A, B, C Enter the desired seat (row number followed by seat letter, e.g., 3A) or enter 'exit' to end: 9A Seat 9A has been successfully booked.
- The “display_available_seats” function displays the available seats for each row based on the current state of theÂ
seats dictionary. - The “book_seat” function handles the seat booking process by prompting the user to enter a seat and updating the seat status accordingly.
- The program initializes the “seats” dictionary to represent the initial state of the seats as available.
- It then displays the available seats and initiates the booking process by calling the “book_seat” function.
- After the booking process is complete, the program thanks the user for using the bus seat booking system.
SOURCE CODE
def display_available_seats(seats):
for row in seats:
available_seats = [seat for seat, status in seats[row].items() if status == 'available']
print(f"Row {row}: Available seats - {', '.join(available_seats)}")
def book_seat(seats):
while True:
choice = input("Enter the desired seat (row number followed by seat letter, e.g., 3A) or enter 'exit' to end: ")
if choice.lower() == 'exit':
break
row = int(choice[:-1])
seat = choice[-1].upper()
if row in seats and seat in seats[row]:
if seats[row][seat] == 'available':
seats[row][seat] = 'booked'
print(f"Seat {choice} has been successfully booked.")
else:
print(f"Seat {choice} is already booked. Please choose another seat.")
else:
print("Invalid seat selection. Please enter a valid seat number.")
display_available_seats(seats)
# Initialize the seats
seats = {row: {seat: 'available' for seat in ['A', 'B', 'C']} for row in range(1, 11)}
# Start the booking process
print("Welcome to the bus seat booking system!")
display_available_seats(seats)
book_seat(seats)
print("Thank you for using the bus seat booking system.")
For more practice, visit our other Python challenges!