尝试练习 python atm 并有一个循环代码,但是带有循环或 if 的东西;坏了,我不知道是什么。据我所知,这段代码应该可以工作,但是在 VSC 上它无法正确输出,不断给出第 18 行 {print("Thank you for your order, your Pizza of ", Members, "is being Preparation")} 并重新启动,无论是否重新启动=假。
小伙伴们有什么想法吗?
restart = True # variable to make it restart
def start() : #start pos to return to
#Pizza time!
ingredients = ['Mozzerala', 'basil', 'tomato', 'garlic', 'olive oil']
print(ingredients)
#above is the base pizza, below is where you add extra ingredients
extra = (input("input extra ingredients here;"))
print(ingredients, "with addional", extra)
ingredients.append(extra)
print(ingredients)
rem = (input("input undesired ingredients here;"))
print(ingredients, "without", rem)
ingredients.remove(rem)
print(ingredients)
final = input('is this correct?') #confirmation check
if final == ('yes') or ('y') or ("confirm") or ("Yes"):
restart = False
print("Thank you for your order, your pizza of ", ingredients, "is being prepared") #will no longer loop as restart is false
elif final == ("no") or ("No") or ("Wrong"):
print("Sorry, Restarting order")
else:
print('Restarting order')
while restart == True: # this will loop until restart is set to be False
start()
您在
restart
中分配给的 start()
变量是该函数的局部变量,与您在第一行分配给的文件级 restart
不同,并签入您的 while
状况。
为了防止这种情况,您的函数
start
可以返回 True
或 False
来指示您是否希望循环继续。然后,您可以将最后一行替换为:
while restart == True:
restart = start()
使用
break
关键字(并将代码从 start()
直接移动到 while
循环中)是控制此问题的另一种方法。