Python Movie Ticket Booking System: Design, Code Walkthrough, and Usage Guide
This article presents a complete Python tutorial for building a command‑line movie ticket reservation system, covering the overall architecture, visual demonstration, detailed code explanations for data storage, seat selection, film selection, and the main controller, along with runnable code snippets.
This guide demonstrates how to create a functional movie ticket booking application using Python, suitable for beginners learning basic programming concepts, data structures, and object‑oriented design.
Effect Display : The system shows a simple textual interface where users can view available movies and seat maps.
Overall Structure Diagram : An image (not reproduced here) outlines the relationship between four modules – infos.py (movie data), seat_booking.py (seat management), film_selector.py (film selection), and main.py (controller).
Code Breakdown
3.1 infos.py
Movie information is stored as a list of dictionaries, each containing 'name' , 'symbol' (ASCII art), and 'seats' (a 6×8 matrix of '○' for free and '●' for booked seats).
<code>infos = [
{
'name': '泰坦尼克号',
'symbol': '''+==================== 泰坦尼克号 =====================+
...
+===================== Titanic =====================+''',
'seats': [[...], [...], ...]
},
{...},
...
]</code>3.2 seat_booking.py
This module defines the SeatBooking class with methods to display seat status ( check_bookings ), obtain valid row/column input ( get_row , get_col ), book a specific seat ( book_seat ), and automatically reserve the frontmost available seat ( book_seat_at_front ).
<code>class SeatBooking:
def check_bookings(self, seats):
print("正在为您查询该场次电影的预订状态...")
...
def get_row(self):
input_row = input("预订第几排的座位呢?请输入 1~6 之间的数字")
...
def get_col(self):
input_column = input("预订这一排的第几座呢?请输入 1~8 之间的数字")
...
def book_seat(self, seats):
while True:
row = self.get_row()
column = self.get_col()
if seats[row][column] == '○':
seats[row][column] = '●'
print("预订成功!座位号:{}排{}座".format(row+1, column+1))
break
else:
print("这个座位已经被预订了哦,试试别的吧")
def book_seat_at_front(self, seats):
for row in range(6):
for column in range(8):
if seats[row][column] == '○':
seats[row][column] = '●'
print("预订成功!座位号:{}排{}座".format(row+1, column+1))
return
print("非常抱歉🥺,所有座位都被订满了,无法为您保留座位")
</code>3.3 film_selector.py
The FilmSelector class presents the list of movies, prompts the user to choose a film or exit ( 'x' ), and validates the input.
<code>class FilmSelector:
def display_options(self, films):
print("今日影院排片列表:")
for i in range(len(films)):
print(f"{i+1} - {films[i]['name']}")
print('x - 退出')
def get_choice(self, films):
valid_choice = [str(i+1) for i in range(len(films))]
valid_choice.append('x')
choice = input('你的选择是?')
while choice not in valid_choice:
choice = input('没有按照要求输入哦,请重新输入')
return choice
</code>3.4 main.py
The Controller class ties everything together: it welcomes the user, lets them select a film, shows the film’s ASCII art, offers two booking methods (specific seat or frontmost seat), and finally says goodbye.
<code>class Controller:
def __init__(self, infos):
self.films = infos
self.welcome()
self.choose_film()
if self.choice != 'x':
self.choose_seat()
self.bye()
...
</code>Running the script launches an interactive console where users can view movies, see seat maps, and reserve seats, illustrating basic Python I/O, list manipulation, and class‑based design.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.