[英]Append in Python loop
我在查找列表 append 的位置时遇到了问题,因此当它通过循环时,这些值会不断累积在列表中。 该代码执行以下操作:
我添加了一个空列表selected=[]并尝试 append 它每次提示用户进行选择。 我在哪里犯错?
Newyork=['Mozarella','Pepperoni','Basil','Green Pepper']
Veggie=['Mozarella', 'Mushroom', 'Green Pepper', 'Onion']
Margarita=['Spicy Tomato Sauce', 'Mozarella']
BBQ=['Mozarella', 'BBQ Sauce','Grilled Chicken', 'Onion' ]
Extra=['Olive','Salami','Sausage','Corn']
extselect=[]
selected=[]
Price_newyork= 10
Price_veggie= 12
Price_margarita= 8
Price_BBQ= 15
print("Welcome to Pizza MIS")
print("---------------------------Menu--------------------------")
print("Newyork:", Newyork)
print("Veggie:", Veggie)
print("Margarita:", Margarita)
print("BBQ:", BBQ)
print("Extra's:", Extra)
def main():
selection=input("Which pizza do you prefer?: ")
selected=selected.append(selection)
extra= input('Would you like to add extra ingredients? Yes or No: ')
while extra== 'Yes' or extra=='yes':
extselect=input("Enter the extra ingredient you want to add your pizza: ")
try:
extselect_index=Extra.index(extselect)
if selection=='Newyork' or selection=='newyork':
Newyork.insert(0,extselect)
print ("Here is your new selection:", Newyork)
extra=input('Do you want to add another ingredient? (Yes or No): ')
if selection=='Veggie' or selection=='veggie':
Veggie.insert(0,extselect)
print ("Here is your new selection:", Veggie)
extra=input('Do you want to add another ingredient? (Yes or No): ')
if selection=='Margarita' or selection=='margarita':
Margarita.insert(0,extselect)
print ("Here is your new selection:", Margarita)
extra=input('Do you want to add another ingredient? (Yes or No): ')
if selection=='BBQ' or selection=='bbq':
BBQ.insert(0,extselect)
print ("Here is your new selection: ", BBQ)
extra=input('Do you want to add another ingredient? (Yes or No): ')
except ValueError:
print("That item was not found in the Extra list")
extra=input('Do you want to add an extra ingredient? (Yes or No): ')
try:
if selection== 'Newyork' or selection=='newyork':
print("Here is your selection: ")
print(Newyork)
price(selection)
if selection=='Veggie' or selection=='veggie':
print("Here is your selection: ")
print(Veggie)
if selection== 'Margarita' or selection=='margarita':
print("Here is your selection: ")
print(Margarita)
if selection== 'BBQ' or selection=='bbq':
print("Here is your selection: ")
print(BBQ)
again=input('Do you want to order another pizza? (Yes or No) ')
if again=='Yes':
main()
else:
print('Bye')
except ValueError:
print("That item was not found in the list ")
def price(selection):
if selection== 'Newyork' or selection=='newyork':
print('It will cost USD',Price_newyork)
elif selection== 'Veggie' or selection=='veggie':
print('It will cost USD',Price_veggie)
elif selection== 'Margarita' or selection=='margarita':
print(Price_margarita)
elif selection== 'BBQ' or selection=='bbq':
print('It will cost USD',Price_BBQ)
else:
print('Enter again')
main()
在您的代码中,您使用整个比萨饼类别作为用户输入,而用户只会输入该类别中的一个比萨饼。
selection=input("Which pizza do you prefer?: ")
同样在第二行中,您执行了selected=selected.append(selection)
作为结果 selected 将是 None 因为列表上的“.append()”操作返回 None 因为它修改列表而不是返回新列表。
继续前进,您在 while 循环中使用extselect_index=Extra.index(extselect)
来检查选定的 Extra 项目是否在 Extra 列表中,如果它不在,则使用 except 块捕获错误。 可以简单地通过 if-else 语句来完成,如下所示:
if extselect in Extra:
**code**
else:
print("not found in the list")
在此之后,您使用了不同的“if”语句,条件为“selection == 'Newyork'”,正如我之前所说的,现在使用 select 一个类别,他将从该类别中取出一个披萨。 这里的条件应该是:
if selection in Newyork:
selected.insert(0,extselect)
print ("Here is your new selection:", Newyork)
extra=input('Do you want to add another ingredient? (Yes or No):')
if extra == 'yes':
continue
else:
break
上面的代码片段将查看所选比萨饼是否属于 Newyork 类别,如果为真,它将 append 将 Extra topping 选择到“选定”列表中,因为它是我们用来维护所选内容的列表。 您可以对每个 if 语句执行相同的操作,并将其他 if 语句更改为“elif”。
在第二个 try 块中,以相同的方式修改 if 语句,并将字符串传递给“price()”function,而不是直接传递变量名。 在价格 function 内,它使用字符串而不是变量来测试选择。
它应该可以解决您在使用上述代码时遇到的任何问题。
Newyork=['Mozarella','Pepperoni','Basil','Green Pepper']
Veggie=['Mozarella', 'Mushroom', 'Green Pepper', 'Onion']
Margarita=['Spicy Tomato Sauce', 'Mozarella']
BBQ=['Mozarella', 'BBQ Sauce','Grilled Chicken', 'Onion' ]
Extra=['Olive','Salami','Sausage','Corn']
extselect=[]
selected=[]
Price_newyork= 10
Price_veggie= 12
Price_margarita= 8
Price_BBQ= 15
print("Welcome to Pizza MIS")
print("---------------------------Menu--------------------------")
print("Newyork:", Newyork)
print("Veggie:", Veggie)
print("Margarita:", Margarita)
print("BBQ:", BBQ)
print("Extra's:", Extra)
def main():
selection=input("Which pizza do you prefer?: ")
selected.append(selection)
extra= input('Would you like to add extra ingredients? Yes or No: ')
while extra== 'Yes' or extra=='yes':
extselect=input("Enter the extra ingredient you want to add your pizza: ")
try:
if extselect in Extra:
if selection in Newyork:
extselect.insert(0,extselect)
print ("Here is your new selection:", selected)
extra=input('Do you want to add another ingredient? (Yes or No): ')
if extra == 'yes' or extra == 'Yes':
continue
else:
break
if selection in Veggie:
extselect.insert(0,extselect)
print ("Here is your new selection:", selected)
extra=input('Do you want to add another ingredient? (Yes or No): ')
if extra == 'yes' or extra == 'Yes':
continue
else:
break
if selection in Margarita:
extselect.insert(0,extselect)
print ("Here is your new selection:", selected)
extra=input('Do you want to add another ingredient? (Yes or No): ')
if extra == 'yes' or extra == 'Yes':
continue
else:
break
if selection in BBQ:
extselect.insert(0,extselect)
print ("Here is your new selection: ", selected)
extra=input('Do you want to add another ingredient? (Yes or No): ')
if extra == 'yes' or extra == 'Yes':
continue
else:
break
else:
print(f'{extselect} is Not available')
except ValueError:
print("That item was not found in the Extra list")
extra=input('Do you want to add an extra ingredient? (Yes or No): ')
try:
if selection in Newyork:
print("Here is your selection: ")
print(selected)
price('Newyork')
elif selection in Veggie:
print("Here is your selection: ")
print(selected)
price('veggie')
elif selection in Margarita:
print("Here is your selection: ")
print(selected)
price('margarita')
elif selection in BBQ:
print("Here is your selection: ")
print(selected)
price('BBQ')
def price(selection):
if selection== 'Newyork' or selection=='newyork':
print('It will cost USD',Price_newyork)
elif selection== 'Veggie' or selection=='veggie':
print('It will cost USD',Price_veggie)
elif selection== 'Margarita' or selection=='margarita':
print(Price_margarita)
elif selection== 'BBQ' or selection=='bbq':
print('It will cost USD',Price_BBQ)
else:
print('Enter again')
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.