我一直在尝试简化此代码,在上一个问题中,我问用户一个可能的解决方案,但我无法使用列表列表,下面是我正在尝试简化的代码:
option = int(input("Table to serve "))
while option != 0:
menu()
if option == 1:
option2 = int(input("Put the code of the food (0 to END): "))
while option2 != 0:
menu()
x = option2 - 1
table1.append((names[x],prices[x],types[x]))
option2 = int(input("Put the code of the food (0 to END): "))
如您所见,它是为单个选项定义的,但是我有6个选项,如果我将其复制并粘贴6次,代码将很长,我如何在考虑到以下情况的情况下简化函数呢? / p>
table1.append((names[x],prices[x],types[x]))
表的名称取决于变量'option'
谢谢。
编辑:
def files():
try:
file = open("menu.txt","r",1,"utf-8")
except:
print("file not found")
line = file.readline().strip()
while line != "":
parts = line.split(";")
name = parts[0]
price = int(parts[1])
type = parts[2]
names.append(name)
prices.append(price)
types.append(type)
line = file.readline().strip()
def menu():
for i in range(len(names)):
print(i+1," ",names[i], " ", prices[i], " ",types[i]," \n")