我对编码有点陌生,因为我在学校没有得到适当的教育,所以我尝试在校外学习。这是我的菜单代码
Sauces = ("\nSauces: \n Cranberry Sauce \n Barbecue Sauce \n Brown Sauce \n Horseradish \n Hot Sauce \n Mayonnaise \n Mustard \n Ketchup \n No Sauce")
Starters = ("\nStarters: \n Calamary \n Chilli Prawn Bruschetta \n Chicken Satay \n 5 Chicken Drumsticks \n Smoked Salmon Cocktial \n Backed Mushrooms With Festive Cheese \n Soup Served In Bread \n Arancini \n Garlic Bread")
while True:
try:
NumberPeople = int(input("How many people are here today? "))
except ValueError:
print("Sorry I didn't understand that. Please try again.")
continue
if NumberPeople < 0:
print("Sorry, you cannot have negative amount of people.")
continue
if NumberPeople < 1:
print("Sorry, you cannot have nobody coming.")
continue
else:
break
if NumberPeople < 3:
print("Thank you. There is a table free for you at Table 2")
elif NumberPeople < 5:
print("Thank you. There is a table free for you at Table 1")
elif NumberPeople < 7:
print("Thank you. There is a table free for you at Table 3")
elif NumberPeople < 21:
print("Thank you. There is a table free for you at Table 8")
else:
print("Sorry we don't have enough space for that many people")
print(Starters)
MyOrder = []
for i in range(NumberPeople):
while True:
try:
StarterMenu = input ("\nPerson %i, what would you like? " % (i + 1))
except ValueError:
print("Sorry I didn't understand. Please try again.")
continue
if StarterMenu == "Calamary":
print(Sauces)
SauceChoice = input("\nWhat sauce do you want? ")
if SauceChoice == "Cranberry Sauce":
MyOrder.append("Calamary with Cranberry Sauce")
break
elif SauceChoice == "Barbecue Sauce":
MyOrder.append("Calamary with Barbecue Sauce")
break
elif SauceChoice == "Brown Sauce":
MyOrder.append("Calamary with Brown Sauce")
break
elif SauceChoice == "Horseradish":
MyOrder.append("Calamary with Horseradish")
break
elif SauceChoice == "Hot Sauce":
MyOrder.append("Calamary with Hot Sauce")
break
elif SauceChoice == "Mayonnaise":
MyOrder.append("Calamary with Mayonnaise")
break
elif SauceChoice == "Mustard":
MyOrder.append("Calamary with Mustard")
break
elif SauceChoice == "Ketchup":
MyOrder.append("Calamary with Ketchup")
break
elif SauceChoice == "No Sauce":
MyOrder.append("Calamary with No Sauce")
break
else:
print("Sorry I didn't understand. Please try again")
continue
else:
print("Okay No Starter")
上面的所有内容似乎都运行顺利,但下面的代码遇到了我似乎不理解的问题。我查看了这个网站,发现我应该这样做,但当我这样做时,我得到了类型错误:列表标记必须是整数或切片,而不是str.
print ("Order is being processed, next order:\n")
MyOrder.append({ 'SauceChoice': SauceChoice })
print('The orders has been processed with these data:')
for i in range(NumberPeople):
order = MyOrder[i]
print (' - Person', i + 1, 'wants ', StarterMenu, 'with %i ' % MyOrder['SauceChoice'])
你的问题是:
您正在使用字符串作为索引,而不是整数变量
SauceChoice
。字符串不是列表/元组的合法索引MyOrder定义为一个列表。当您试图访问SauceChoice的值时,必须指定键“SauceChoice”的索引
相关问题 更多 >
编程相关推荐