我正在学习Python,但我仍然是一个初学者,这是我的问题:
toppinglist = []
instock = ['Cheese', 'Tomato', 'Chicken', 'Mushroom']
Outofstock = ['Beef', 'Pork', 'Onions']
wanttoppings_q = input('Do you want toppings on your pizza? Yes/No')
wanttoppings_q = wanttoppings_q.title()
while wanttoppings_q == 'Yes':
requestedtopping = input('Enter One Topping:')
toppinglist.append(requestedtopping)
anythingelse = input('Anything Else? Yes/No' )
anythingelse = anythingelse.title()
toppinglist = []
if anythingelse == 'No':
break
print(toppinglist)
使用这段代码,基本上的目标是询问你想要什么披萨配料,然后将其添加到列表中,我尝试使用我的大脑并想出了这个。一切都很好,虽然循环很好,但后来我打印了顶部列表,里面什么也没有。也许我使用附加错误?请帮忙
在打印之前,您正在重置 toppinglist = [],因此将其删除,这样一切都会正常工作
toppinglist = []
instock = ['Cheese', 'Tomato', 'Chicken', 'Mushroom']
Outofstock = ['Beef', 'Pork', 'Onions']
wanttoppings_q = input('Do you want toppings on your pizza? Yes/No ')
wanttoppings_q = wanttoppings_q.title()
while wanttoppings_q == 'Yes':
requestedtopping = input('Enter One Topping: ')
toppinglist.append(requestedtopping)
anythingelse = input('Anything Else? Yes/No ')
anythingelse = anythingelse.title()
if anythingelse == 'No':
break
print(toppinglist)