import re
class ATM:
"""自动售票机"""
data = {
'2020-07-19': [
{
'紧急救援': {
'9:30': list(range(1, 101)), '10:40': list(range(1, 101)), '14:30': list(range(1, 101))}},
{
'急先锋': {
'10:30': list(range(1, 101)), '11:40': list(range(1, 101)), '15:30': list(range(1, 101))}},
{
'花木兰': {
'8:30': list(range(1, 101)), '10:40': list(range(1, 101)), '14:30': list(range(1, 101))}},
{
'我和我的祖国': {
'10:30': list(range(1, 101)), '11:40': list(range(1, 101)), '15:30': list(range(1, 101))}},
{
'千与千寻': {
'10:30': list(range(1, 101)), '12:40': list(range(1, 101)), '15:30': list(range(1, 101))}},
{
'大鱼海棠': {
'8:30': list(range(1, 101)), '11:40': list(range(1, 101)), '15:30': list(range(1, 101))}},
{
'十二生肖2': {
'9:30': list(range(1, 101)), '11:40': list(range(1, 101)), '15:30': list(range(1, 101))}},
{
'最后的魁拔': {
'10:30': list(range(1, 101)), '14:40': list(range(1, 101)), '18:30': list(range(1, 101))}}]}
today = '2020-5-5'
user = ""
password = ""
is_login = False
is_func = True
option = ""
option_key = ""
scene = ""
scene_key = ""
seat = ""
seat_key = ""
seat_value = ""
def __init__(self):
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("请输入用户名(root):")
self.password = input("请输入密码(123):")
if self.user == "root" and self.password == "123":
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 = tip_option + str(i + 1) + "、《" + data_option[0] + "》 "
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)):
had_format = '{:^3}'
x_format = '{:>2}'
y_format = '{:<2}'
if (i + 1) % 10 != 0:
if str.isdigit(str(data_seat[i])):
x = data_seat[i] // 10 + 1
y = data_seat[i] % 10
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
y = data_seat[i] % 10
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
y = self.seat_value % 10
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):
x = re.findall(r'[0-9]', self.seat)[0]
y = re.findall(r'[0-9]', self.seat)[1]
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()
while app.is_func:
app.main()
app.loading()