[英]How do I keep two arrays parallel arrays?
用户需要输入表格编号(表格编号从 0 到 19)。 如果该表不可用(保留),则通知用户所选表不可用。 如果该表可用,则询问名称(名称为单个单词,无空格)并将该表标记为已保留。 现在我需要保留两个 arrays (并行 arrays,),两者都是大小 20,boolean 类型之一(如果保留,则为空的其他类型的保留/可用的保留字符串)
tableNum = []
def reserve():
global table
global name
global tableNum
avaible = False
tablenum = int(input("Enter a number: "))
if not tablenum in tableNum:
name = input("Table is avaiable, please enter your name: ")
else:
print("Table is unavaiable")
while(True):
print("1- Reserve a Table")
print("2- Clear Reservation")
print("3- Report")
print("0- Exit")
choice = int(input("Choose a option "))
if choice == 1:
reserve()
您可以在不保留所有表的列表的情况下执行此操作。 Append 保留作为键、值对的字典。 然后,您可以检查输入是否与预订字典中的键匹配。 然后,您可以使用相反的方法删除预订。
reservationDictionary ={}
def reserve():
global reservationDictionary
tablechoice = int(input('Tables are numbered 0-19, Choose a table number'))
if tablechoice not in reservationDictionary.keys():
name = input("Table is available, enter your Name: ")
reservationDictionary[tablechoice] = name
else:
print("Table is unavailable")
更新以删除预订:
def removeReservation():
global reservationDictionary
removetable = int(input('Choose table number to remove reservation for that table'))
if removetable not in reservationDictionary.keys():
print("This table doesn't have a reservation")
else:
del reservationDictionary[removetable]
print("Reservations for table {} has been deleted".format(removetable))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.