Fundamentals 11 min read

Python Movie Ticket Booking System: Code Walkthrough and Implementation

This article presents a step‑by‑step Python tutorial for building a console‑based movie ticket reservation system, covering data structures for film information, seat‑booking logic, film selection, and a controller that integrates all components into a functional application.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Movie Ticket Booking System: Code Walkthrough and Implementation

This tutorial walks through building a console‑based movie ticket reservation system in Python.

Effect demonstration : The program displays a visual representation of seat availability for each film.

Overall structure diagram : (image omitted) shows the relationship between the data module, selector, booking class, and controller.

Code breakdown – infos.py defines a list of dictionaries, each containing a film’s name, ASCII‑art symbol, and an 6×8 seat matrix where “○” indicates an empty seat and “●” a booked seat.

infos = [
  {
    'name': '泰坦尼克号',
    'symbol': ''
+==================== Titanic ====================+
  ,▄▄▄▄▄▪   ▄▄▄▄▄  ▄▄▄·   ▐ ▄ ▪      ▄·
  •██   ██  •██   ▐█ ▀█  •█▌▐█  ██  █▌▪
  ▐█.▪ ▐█·  ▐█. ▪▄█▀▀█  ▐█▐▐▌  ▐█· ██ ▄▄
  ▐█▌ ·▐█▌  ▐█▌· ▐█ ▪▐▌ ██▐█▌  ▐█▌ ▐███▌
  ▀▀▀  ▀▀▀  ▀▀▀   ▀  ▀  ▀▀ █  ▪▀▀▀ ·▀▀▀ 
+===================== Titanic ======================+
',
    'seats': [[
      '○','○','○','○','○','○','○','○'],
      ['○','○','○','○','●','○','○','●'],
      ['○','○','●','○','●','○','○','○'],
      ['○','○','●','○','○','○','○','●'],
      ['○','○','●','○','○','○','○','●'],
      ['●','○','○','○','●','●','●','●']
    ]
  },
  {
    'name': '雨人',
    'symbol': ''
+====================== 雨人 ========================+
  ,---.    .--.  ,..-. .-.
  | .-.  /\ /\ \|(||  \| | |\    /| /\ /\ |  \| | 
  |`-'/ / /__/ \(_)|   | | |(\  / |/ /__/ \|   | |
  |   (  |  __  || || |\  | (_)\/  ||  __  ||\  | 
  |\|\ \| |  |)|| || | |)| | \  /|| |  |)|| | |)\ 
  |_|\)\|_|  (_)`-/(  (_) | |\/| ||_|  (_)\  (_) 
+===================== Rain Man =====================+
',
    'seats': [[
      '○','○','○','○','●','○','○','●'],
      ['○','○','○','●','●','○','○','○'],
      ['○','●','○','○','○','○','○','○'],
      ['○','○','○','○','○','○','○','○'],
      ['○','○','○','○','○','○','○','○'],
      ['○','○','○','○','●','○','○','○']
    ]
  },
  {
    'name': '卡门',
    'symbol': ''
+======================= 卡门 ========================+
  ,▄█▄    ██   █▄▄▄▄ █▀▄▀█ ▄███▄      ▄   
  █▀  ▀▄  █ █  █  ▄▀ █ █ █ █▀   ▀      █  
  █   ▀  █▄▄█ █▀▀▌  █ ▄ █ ██▄▄    ██   █ 
  █▄  ▄▀ █  █ █  █  █   █ █▄   ▀█ █  █ 
  ▀███▀     █   █      █  ▀███▀   █  █ █ 
            █   ▀      ▀           █   ██ 
          ▀                                 
+====================== Carmen =====================+
',
    'seats': [[
      '○','○','○','○','○','○','○','○'],
      ['○','○','●','●','○','○','●','●'],
      ['○','○','○','○','○','○','●','○'],
      ['○','○','○','○','○','○','○','○'],
      ['○','○','○','●','○','○','○','●'],
      ['○','○','○','○','○','○','○','○']
    ]
  },
  {
    'name': '机器人总动员',
    'symbol': ''
+==================== 机器人总动员 ====================+
  (`\ .-') )/`  (-.                 ('-.   
  `.( OO ),( OO ).-.             _(  OO)  
,--./  .--.   /. --. /,--.      ,--.   (,------.
|      |  |   |\-.  \  |  |.-') )  |  .---'
|  |   |  |,.-'-'  |  | |  | OO )|  | OO )
|  |.|\  | |  |)|| | | | |\|  |\  |  |\| 
|_| \)\|_|  (_)`-/(  (_) | |\/| ||_|  (_)\  (_) 
+====================== WALL·E =====================+
',
    'seats': [[
      '●','○','○','○','○','○','○','○'],
      ['●','○','○','○','○','○','○','●'],
      ['○','○','●','○','●','○','●','○'],
      ['○','○','○','○','○','○','○','○'],
      ['○','○','○','●','○','○','○','●'],
      ['●','●','○','○','○','●','○','○']
    ]
  },
  {
    'name': '黑客帝国',
    'symbol': ''
+===================== 黑客帝国 =====================+
  ______            __  ___      __       _     
 /_  __/ /_  ___   /  |/  /___ _/ /______(_) __
  / / / __ \/ _ \ / /|_/ / __ `/ __/ ___/ / /_/ /
 / / / / / /  __// /  / / /_/ / /_/ /  / / / __/ 
/_/ /_/ /_/\___//_/  /_/\__,_/\__/_/  /_/_/    
+==================== The Matrix ====================+
',
    'seats': [[
      '○','●','○','○','○','○','○','○'],
      ['○','○','○','●','●','○','○','●'],
      ['○','○','○','○','○','○','○','○'],
      ['○','○','○','○','○','○','○','○'],
      ['○','○','○','○','○','○','○','●'],
      ['○','○','●','○','○','○','○','○']
    ]
  }
]

Code breakdown – seat_book.py implements the SeatBooking class with methods to display seat status, validate row and column input, book a specific seat, and automatically allocate the frontmost available seat.

import time

class SeatBooking:
  # 展示所有座位的预订信息
  def check_bookings(self, seats):
    print("正在为您查询该场次电影的预订状态...")
    time.sleep(0.7)
    print('从上到下为 1~6 排,从左至右为 1~8 座')
    print("======================")
    for row in seats:
      time.sleep(0.1)
      print('  '.join(row))
    print("======================")
    time.sleep(0.7)

  # 获取符合要求的行索引
  def get_row(self):
    input_row = input("预订第几排的座位呢?请输入 1~6 之间的数字")
    valid_row = [str(i + 1) for i in range(6)]
    while input_row not in valid_row:
      input_row = input('没有按要求输入哦,请输入 1~6 之间的数字')
    row = int(input_row) - 1
    return row

  # 获取符合要求的列索引
  def get_col(self):
    input_column = input('预订这一排的第几座呢?请输入 1~8 之间的数字')
    valid_column = [str(i + 1) for i in range(8)]
    while input_column not in valid_column:
      input_column = input('没有按要求输入哦,请输入 1~8 之间的数字')
    column = int(input_column) - 1
    return column

  # 预订指定座位
  def book_seat(self, seats):
    while True:
      row = self.get_row()
      column = self.get_col()
      # 指定座位没有被预订
      if seats[row][column] == '○':
        print("正在为您预订指定座位...")
        time.sleep(0.7)
        seats[row][column] = '●'
        print("预订成功!座位号:{}排{}座".format(row + 1, column + 1))
        break
      # 指定座位已经被预订了
      else:
        print("这个座位已经被预订了哦,试试别的吧")
        time.sleep(0.7)

  # 预订最靠前的座位
  def book_seat_at_front(self, seats):
    print("正在为您预订最靠前的座位...")
    time.sleep(0.7)
    # 外循环:遍历 seats 的行
    for row in range(6):
      # 内循环:遍历 seats 的列
      for column in range(8):
        # 若碰到没有被预订的座位
        if seats[row][column] == '○':
          seats[row][column] = '●'  # 预订该座位
          print("预订成功!座位号:{}排{}座".format(row + 1, column + 1))
          return  # 结束函数的执行,返回到它被调用的地方
    # 没有在循环内部结束程序,说明不存在没有被预订的座位
    print("非常抱歉🥺,所有座位都被订满了,无法为您保留座位")

Code breakdown – film_selector.py provides the FilmSelector class that lists available movies, accepts a user’s choice (or “x” to exit), and returns the selection.

import time

class FilmSelector:
  # 展示所有可选项
  def display_options(self, films):
    print("今日影院排片列表:")
    print('+================+')
    # 按行打印每部电影
    for i in range(len(films)):
      print('{} - {}'.format(i + 1, films[i]['name']))
      time.sleep(0.2)
    # 打印退出选项
    print('x - 退出')
    print('+================+')
    time.sleep(0.7)

  # 获取用户的选择
  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 breakdown – main.py defines the Controller class that greets the user, invokes FilmSelector to choose a film, shows the selected film’s ASCII art, presents booking options, and calls SeatBooking methods based on the user’s preference.

import time
from infos import infos
from film_selector import FilmSelector
from seat_booking import SeatBooking

class Controller:
  def __init__(self, infos):
    self.films = infos  # 电影库所有电影
    self.welcome()
    self.choose_film()
    if self.choice != 'x':
      self.choose_seat()
    self.bye()

  # 用户选择想观看的电影
  def choose_film(self):
    selector = FilmSelector()
    selector.display_options(self.films)
    self.choice = selector.get_choice(self.films)

  # 为指定场次预订座位
  def choose_seat(self):
    film = self.films[int(self.choice) - 1]
    name = film['name']
    seats_list = film['seats']
    symbol = film['symbol']
    print('正在为您预订电影《{}》的座位...'.format(name))
    time.sleep(0.7)
    print(symbol)
    time.sleep(0.7)
    print('支持的座位预订方式如下:')
    time.sleep(0.7)
    print('+==========================+')
    print("1 - 指定行列号预定座位")
    print("2 - 给我预订一个最靠前的座位!")
    print('+==========================+')
    time.sleep(0.7)
    method = input('请选择座位预订方式')
    valid_method = ['1','2']
    while method not in valid_method:
      method = input('没有按照要求输入哦,请重新输入')
    booking = SeatBooking()
    booking.check_bookings(seats_list)
    if method == '1':
      booking.book_seat(seats_list)
    else:
      booking.book_seat_at_front(seats_list)

  # 打印欢迎语
  def welcome(self):
    print('+============================+')
    print('+      欢迎来到时光电影院       +')
    print('+============================+')
    time.sleep(0.7)

  # 打印结束语
  def bye(self):
    print('')
    time.sleep(0.7)
    print('+============================+')
    print('+    已经退出系统,下次见!👋    +')
    print('+============================+')

# 实例化 Controller 类
s = Controller(infos)

Running the script launches an interactive session where users can select a movie, view seat maps, and reserve seats either by specifying coordinates or by automatically receiving the nearest front seat.

ticketingOOPExampleconsole-app
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.