Create a cinema seat booking system that shows free seats, stops double bookings and prints a receipt.
Have you ever booked seats at the cinema and watched the seat map change colour as other people grab the best spots? In this project you will build the program behind a tiny cinema called Python Cinema. The player sees which seats are free, picks the ones they want, and gets a receipt with the total price at the end.
This project is all about lists. A list lets one variable hold lots of values, like all the seat names in the cinema. You will use one list for every seat and a second list for the seats that have been booked, and you will move seats between them as the player makes choices.
You will also practise checking for mistakes. What happens if someone types a seat that doesn't exist, or tries to book a seat that is already taken? Your program will spot these problems and give a helpful message, just like a real booking website.
We store every seat name in a list, using square brackets with commas between the items. A second, empty list called booked will remember which seats have been taken. len() tells us how many items a list has.
seats = ["A1", "A2", "A3", "A4", "B1", "B2", "B3", "B4"]
booked = []
print("đŦ Welcome to Python Cinema!")
print("We have", len(seats), "seats tonight.")
đŦ Welcome to Python Cinema! We have 8 seats tonight.
A for loop visits every seat in the list, one at a time. For each seat, we check if it is not in the booked list. Using end=" " inside print() keeps everything on one line with spaces in between, instead of starting a new line each time.
seats = ["A1", "A2", "A3", "A4", "B1", "B2", "B3", "B4"]
booked = ["A2", "B3"]
print("Free seats:", end=" ")
for seat in seats:
if seat not in booked:
print(seat, end=" ")
print()
Free seats: A1 A3 A4 B1 B2 B4
We ask the player which seat they want. .upper() turns their answer into capital letters, so typing a1 works just as well as A1. Then .append() adds the seat to the end of the booked list.
booked = []
choice = input("Pick a seat: ").upper()
booked.append(choice)
print("â
Seat", choice, "is yours!")
print("Booked seats:", booked)
Pick a seat: a1 â Seat A1 is yours! Booked seats: ['A1']
Before booking, we need to make sure the seat really exists and nobody else has it. We use if, elif and else to check each problem in turn. Only when both checks pass do we add the seat to the booked list.
seats = ["A1", "A2", "A3", "A4", "B1", "B2", "B3", "B4"]
booked = ["A2"]
choice = input("Pick a seat: ").upper()
if choice not in seats:
print("Hmm, that seat doesn't exist.")
elif choice in booked:
print("Sorry, seat", choice, "is already taken.")
else:
booked.append(choice)
print("â
Seat", choice, "is yours!")
Pick a seat: a2 Sorry, seat A2 is already taken.
Families often need more than one seat! while True: makes a loop that would go on forever, so we need a way out. When the player types done, the break command jumps straight out of the loop. Everything else in this step is the code you already wrote, now placed inside the loop.
while True:
choice = input("Pick a seat (or type done): ").upper()
if choice == "DONE":
break
print("You picked", choice)
print("Finished booking!")
Pick a seat (or type done): b1 You picked B1 Pick a seat (or type done): b2 You picked B2 Pick a seat (or type done): done Finished booking!
When the loop ends, we show the player their seats and the total price. Each ticket costs 8 dollars, so we multiply the number of booked seats by the price. We use str() to turn the number into text so we can join it to the $ sign with +.
booked = ["A1", "B2"]
price = 8
print("Your seats:", booked)
print("Total price: $" + str(len(booked) * price))
print("Enjoy the movie! đŋ")
Your seats: ['A1', 'B2'] Total price: $16 Enjoy the movie! đŋ
Here is the whole program with every step put together. If something isn't working in your version, compare it with this one line by line. Check the spelling, the colons at the end of if and while lines, and that the indentation (the spaces at the start of lines) matches.
seats = ["A1", "A2", "A3", "A4", "B1", "B2", "B3", "B4"]
booked = []
price = 8
print("đŦ Welcome to Python Cinema!")
print("Tonight's movie: The Robot Who Loved Pizza")
while True:
print()
print("Free seats:", end=" ")
for seat in seats:
if seat not in booked:
print(seat, end=" ")
print()
choice = input("Pick a seat (or type done): ").upper()
if choice == "DONE":
break
elif choice not in seats:
print("Hmm, that seat doesn't exist.")
elif choice in booked:
print("Sorry, seat", choice, "is already taken.")
else:
booked.append(choice)
print("â
Seat", choice, "is yours!")
print()
print("Your seats:", booked)
print("Total price: $" + str(len(booked) * price))
print("Enjoy the movie! đŋ")
This is what it looks like when someone plays. The words after each question are what the player typed.
đŦ Welcome to Python Cinema! Tonight's movie: The Robot Who Loved Pizza Free seats: A1 A2 A3 A4 B1 B2 B3 B4 Pick a seat (or type done): a2 â Seat A2 is yours! Free seats: A1 A3 A4 B1 B2 B3 B4 Pick a seat (or type done): B3 â Seat B3 is yours! Free seats: A1 A3 A4 B1 B2 B4 Pick a seat (or type done): A2 Sorry, seat A2 is already taken. Free seats: A1 A3 A4 B1 B2 B4 Pick a seat (or type done): C9 Hmm, that seat doesn't exist. Free seats: A1 A3 A4 B1 B2 B4 Pick a seat (or type done): done Your seats: ['A2', 'B3'] Total price: $16 Enjoy the movie! đŋ
Finished? Brilliant! Real programmers are always improving their programs. Try one of these challenges to make the project truly yours.
Stuck on a step? These lessons explain the Python ideas used in this project.