这是我为学校项目撰写的程序。在最后一个其他功能中,您可以看到我尝试删除之前的披萨订单,因为用户输入了拼写错误。但是,使用简单的 - =为 - =:' str'授予TypeError:不支持的操作数类型。和' str'。这是我第一次写任何除了Scratch(:P)以外的任何内容,所以我们将不胜感激。
item_names = ["0","Super Supreme\n","Meat Lovers\n","BBQ Chicken & Bacon\n",\
"Apricot Chicken\n","Satay Chicken\n","Pepperoni Feast\n",\
"Beef Italiano\n","Seafood Deluxe\n","BBQ Hawaiian\n",\
"Peperoni Lover\n","Veg Supreme\n","Trio of Cheese\n",\
"Beef Hot 'n Spicy\n","Roma\n","Hawaiian\n","Classic Cheese\n",\
"Veg Hot 'n Spicy\n","Ham and Cheese\n","Beef and Onion\n",\
"Margherita\n","Large Fries\n","Garlic Bread\n","Onion Rings\n",\
"5 Spicy Chicken Wings","Dipping Sauce (","Orange Juice\n",\
"Bottled Water\n","355ml Soft Drink (","1.5L Soft Drink ("];
item_prices = [0,10.50,10.50,10.50,10.50,10.50,10.50,10.50,10.50,8.00,8.00,\
8.00,8.00,8.00,8.00,5.00,5.00,5.00,5.00,5.00,5.00,3.50,3.50,\
3.50,5.00,0.50,3.50,3.50,1.90,3.90];
print('Hello and welcome to the Shinanigan Pizzary!')
start =1
while start == 1:
menu_prompt = str(input("Would you like to open the menu? y/n\n"))
if menu_prompt == "y":
start =0
menu()
running_total =0
running_names =''
loop1 =1
while loop1 == 1:
try:
items_number = int(input("Please insert your desired item's number\n"))
running_total += float((item_prices[items_number]))
running_names += item_names[items_number]
query1 = str(input("Is that all? y/n\n\n"))
except IndexError:
print("Please enter a valid item number.\n\n")
if query1 == "y":
loop1 =0
print(running_names)
print("Running total:","$",(running_total))
print("\nThank you for coming! Your order is being made!")
elif query1 == "n":
print(running_names)
print("Running total:","$",(running_total))
else:
running_total -=float((item_prices[items_number]))
running_names -= item_names[items_number]
print("Please enter 'y' or 'n'")
elif menu_prompt == "n":
print("Okay. Thanks for coming. See you next time.")
break
答案 0 :(得分:0)
使用str.replace将字符串替换为空字符串:
running_names = running_names.replace(item_names[items_number],"")
In [6]: running_names = "Hawaiian\n mistake\n"
In [7]: print( running_names.replace("mistake\n",""))
Hawaiian
你不能减去两个字符串。