如何在基于python的影院程序中实现高效的票务系统?



下面的图片是我的电影程序中可用的票,有人知道让用户输入他们需要的票和每张票的编号的最好方法吗?我完全迷路了。我能想到的唯一方法就是询问每一张票的情况,但这种方法效率很低。

("""We have a number of ticket options available
-----------------Tickets-----------------
1) Standard : £10.20
2) Child : £7.70
3) Student : £8.20
4) Senior Ticket (60+) : £6.80
-----------------------------------------""")
Tickets, number = input("Please enter the number corresponding to your ticket choice followed by the number of tickets you would like to purchase: ").split()
choice = input("Would you like to purchase more tickets?" )
if choice == "yes":
Tickets1 = input("Please enter the number corresponding to your additional ticket choice followed by the number of tickets you would like to purchase: ").split()

最直接的方法是收集元组(index, count),其中index是票号(但也可以是票名),count是该类型的票号。

确保验证所有输入,并重新检查是否出了问题。

UPD:您可能应该使用while循环来收集票据答案

我会将票证请求逻辑放在一个循环中,直到用户说他们已经完成为止。像这样:

print("""We have a number of ticket options available
-----------------Tickets-----------------
1) Standard : £10.20
2) Child : £7.70
3) Student : £8.20
4) Senior Ticket (60+) : £6.80
-----------------------------------------""")
def get_ticket_order():
complete = False
order = []
while not complete:
Tickets, number = input("Please enter the number corresponding to your ticket choice followed by the number of tickets you would like to purchase: ").split()
order.append((Tickets, number))
choice = input("Would you like to purchase more tickets?" )
if choice != "yes":
complete = True
return order
order = get_ticket_order()            

order将最终成为一个包含多个子列表的列表,每个子列表都有2个值,1个用于票号,另一个用于数量。为了访问它们,您只需要使用索引提取它们或使用for循环遍历它们。例如:

order = [[4,1], [2,2], [3,1]]
for ticket, quantity in order:
#do something with ticket
# do something with quantity
# or you can extract them individually with indexes
ticket1, quantity1 = order[0] # ticket1 now = 4 quantity1 now = 1
ticket2, quantity2 = order[1] # ticket2 now = 2 quantity2 now = 2

最新更新