注意:
该项目是基于前几个月的世界杯所写的简易购票系统,未加入界面点击操作,只给出终端运行方式
一.介绍 re
在该项目当中,我们将选择“re”库为基本库,来贯穿整个代码运行
用于处理正则表达式。正则表达式是一种用于匹配字符串的模式,可以用于搜索、替换和分割字符串。re库提供了一系列函数和方法,可以用于处理字符串中的模式匹配。
没有了解过的朋友请移步:re库的基本介绍
二. 运行结果展示
三. 完整代码
import re # 判断所选座位(如1-1)时需要用到正则表达式
class ATM:
"""自动售票机"""
data = {'2022-11-27': [ # 今日售票信息 已知信息 八个电影各三个场次各100票
{'小组赛D-F组第二轮 每场200$:法国vs丹麦、阿根廷vs墨西哥、日本vs哥斯达黎加、比利时vs摩洛哥': {'00:00': list(range(1, 101)), '03:00': list(range(1, 101)), '18:00': list(range(1, 101)),'21:00':list(range(1,101))}},
{'小组赛F-H组第二轮 每场200$:克罗地亚vs加拿大、西班牙vs德国、喀麦隆vs塞尔维亚、韩国vs加纳': {'00:00': list(range(1, 101)), '03:00': list(range(1, 101)), '18:00': list(range(1, 101)),'21:00':list(range(1,101))}},
{'小组赛B-D组第三轮 每场200$:伊朗vs美国、威尔士vs英格兰、澳大利亚vs丹麦、突尼斯vs法国': {'伊美 03:00': list(range(1, 101)), '威英 03:00': list(range(1, 101)), '澳丹 23:00': list(range(1, 101)),'突法 23:00':list(range(1,101))}},
{'1/8决赛 每场300$': {'荷兰vs美国 23:00': list(range(1, 101)), '阿根廷vs澳大利亚 03:00': list(range(1, 101)), '法国vs波兰 23:00': list(range(1, 101))}},
{'1/8决赛 每场300$' : {'英格兰vs塞内加尔 03:00': list(range(1, 101)), '日本vs克罗地亚 23:00': list(range(1, 101)), '巴西vs韩国 03:00': list(range(1, 101)),'葡萄牙vs瑞士 03:00':list(range(1,101))}},
{'1/4决赛 每场500$': {'荷兰vs阿根廷 03:00': list(range(1, 101)), '摩洛哥vs葡萄牙 23:00': list(range(1, 101)), '英格兰vs法国 03:00': list(range(1, 101)),'克罗地亚vs巴西 23:00': list(range(1,101))}},
{'半决赛 每场700$': {'法国vs摩洛哥 03:00': list(range(1, 101)),'阿根廷vs克罗地亚 03:00':list(range(1,101))}},
{'季军赛 每场1000$': {'克罗地亚vs摩洛哥 03:00': list(range(1, 101))}},
{'决赛 每场1500$': {'阿根廷vs法国 23:00': list(range(1, 101))}}]}
today = '2022-11-27' # 今天日期 已知信息
user = "" # 用户名 root
password = "" # 密码 123
is_login = False # 用户是否登陆
is_func = True # 系统是否运行
option = "" # 所选电影名称 如:红海行动(3)
option_key = "" # 所选电影索引 则:2
scene = "" # 所选场次 如:8:30(1)
scene_key = "" # 所选场次索引 则:0
seat = "" # 所选座位 如:1-1(1)
seat_key = "" # 所选座位索引 则:0
seat_value = "" # 所选座位索引值(票号) 则:1
def __init__(self): # __init__()方法
self.loading() # 加载函数
def loading(self): # 系统加载函数
is_func = input("Loading(输入任意字符激活)...\n")
if is_func == "exit": # 退出系统
self.is_func = False
print("正在退出系统...")
def login(self): # 用户登陆函数
self.user = input("请输入用户名(Shijiebei):")
self.password = input("请输入密码(520123):")
if self.user == "Shijiebei" and self.password == "520123": # 默认有一个root用户
self.is_login = True
print("已成功登陆!开始选票!\n")
else:
print("密码或用户名输入错误!")
self.login()
def format_tip_option(self): # 比赛选择函数
tip_option = "" # 比赛提示信息
for i in range(len(self.data[self.today])): # 初始化比赛提示信息
data_option = list(self.data[self.today][i])
tip_option = '\n'+tip_option + str(i + 1) + "、《" + data_option[0] + "》 " + '\n'
tip_option = "请选择您要观看的比赛:" + tip_option + '\n'
self.option = input(tip_option) # 所选比赛名称
self.option_key = "" # 所选比赛索引
if str.isdigit(self.option) and 0 < int(self.option) <= len(self.data[self.today]): # 输入内容为比赛提示信息序列
self.option_key = int(self.option) - 1 # 所选比赛索引
self.option = list(self.data[self.today][self.option_key])[0] # 所选比赛名称
elif str.isalpha(self.option) and u'\u4e00' <= self.option <= u'\u9fa5': # 输入内容为比赛名称
for i in range(len(self.data[self.today])):
data_option = list(self.data[self.today][i])
if self.option == data_option[0]:
self.option_key = i # 所选比赛索引
if self.option_key == "": # 没有找到所选比赛索引
print("您选择错误!请重新选择!")
self.format_tip_option() # 比赛选择函数
def format_tip_scene(self): # 场次选择函数
print("已选择的比赛:《" + self.option + "》\n")
tip_scene = "" # 场次信息
data_scene = list(self.data[self.today][self.option_key].values()) # 初始化场次信息
data_option_scene = list(data_scene[0])
for i in range(len(data_option_scene)):
tip_scene = tip_scene + str(i + 1) + "、" + data_option_scene[i] + " "
tip_scene = "请选择比赛场次:" + tip_scene + "\n"
self.scene = input(tip_scene) # 选择场次
if str.isdigit(self.scene) and 0 < int(self.scene) <= len(data_option_scene): # 输入内容为数字
self.scene_key = int(self.scene) - 1
self.scene = data_option_scene[self.scene_key]
else:
for i in range(len(data_option_scene)):
if self.scene == data_option_scene[i]:
self.scene_key = i
if self.scene_key == "":
print("您选择错误!请重新选择!")
self.format_tip_scene() # 场次选择函数
def format_tip_seat(self):
print("已选比赛场次:" + self.scene + '\n')
tip_seat = "" # 初始化座位信息
data_seat = list(list(self.data[self.today][self.option_key].values())[0].values())[self.scene_key]
for i in range(len(data_seat)): # 默认每个场次座位有10列
had_format = '{:^3}' # 已选择
x_format = '{:>2}' # 格式化座位-x(行)
y_format = '{:<2}' # 格式化座位-y(列)
if (i + 1) % 10 != 0:
if str.isdigit(str(data_seat[i])): # 座位索引值为票号
x = data_seat[i] // 10 + 1 # 座位-x(行)
y = data_seat[i] % 10 # 座位-y(列)
if x == 0:
x = 10
y = y - 1
if y == 0:
x = x - 1
y = 10
tip_seat = tip_seat + x_format.format(str(x)) + "-" + y_format.format(str(y)) + "\t"
else: # 座位索引值为用户名
tip_seat = tip_seat + had_format.format('已选择') + "\t"
else:
if str.isdigit(str(data_seat[i])): # 座位索引值为票号
x = data_seat[i] // 10 + 1 # 座位-x(行)
y = data_seat[i] % 10 # 座位-y(列)
if x == 0:
x = 10
y = y - 1
if y == 0:
x = x - 1
y = 10
tip_seat = tip_seat + x_format.format(str(x)) + "-" + y_format.format(str(y)) + "\n\t"
else: # 座位索引值为用户名
tip_seat = tip_seat + had_format.format('已选择') + "\n\t"
self.seat = input("请选择座位:\n\t%s" % tip_seat)
if str.isdigit(self.seat): # 输入内容为座位索引值
self.seat_value = int(self.seat) # 所选座位索引值
x = self.seat_value // 10 + 1 # 座位-x(行)
y = self.seat_value % 10 # 座位-y(列)
if x == 0:
x = 10
y = y - 1
if y == 0:
x = x - 1
y = 10
self.seat = str(x) + '-' + str(y) # 所选座位
elif re.match(r'[0-9]+-+[0-9]+$', self.seat, re.X): # 输入内容为座位(如:1-1 1-10 10-1 10-10)
x = re.findall(r'[0-9]', self.seat)[0] # 座位-x(行)
y = re.findall(r'[0-9]', self.seat)[1] # 座位-y(列)
self.seat_value = (int(x) - 1) * 10 + int(y) # 所选座位索引值
if self.seat_value not in self.data[self.today][self.option_key][self.option][self.scene]: # 没有找到所选座位索引
print("您选择错误!请重新选择!")
self.format_tip_seat() # 座位选择函数
def data_update(self): # 数据更新
self.seat_key = self.data[self.today][self.option_key][self.option][self.scene].index(self.seat_value)
print("已选座位:" + self.seat + '\n')
print('正在出票...')
self.data[self.today][self.option_key][self.option][self.scene][self.seat_key] = self.user # 将所选座位改为用户名
def data_option(self): # 出票
print('''
比赛:《%s》
比赛时间:%s %s
座位:%d号(%s)
出票完成,请别忘记取票
''' % (self.option, self.today, self.scene, self.seat_value, self.seat))
def main(self): # 主函数
print("欢迎使用自动售票机\n")
self.login() # 用户登陆
self.format_tip_option() # 选择比赛
self.format_tip_scene() # 选择场次
self.format_tip_seat() # 选择座位
self.data_update() # 更新数据
self.data_option() # 出票信息
app = ATM() # 实例化ATM对象
while app.is_func: # 系统是否运行
app.main() # app主函数
app.loading() # app加载函数