循环损坏,If 语句/while [重复]

问题描述 投票:0回答:4

我现在正在尝试练习Python并有一个循环代码,但是带有循环或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()
python if-statement while-loop
4个回答
1
投票

您在

restart
中分配给的
start()
变量是该函数的局部变量,与您在第一行分配给的文件级
restart
不同,并签入您的
while 
状况。

为了防止这种情况,您的函数

start
可以返回
True
False
来指示您是否希望循环继续。然后,您可以将最后一行替换为:

while restart == True:
   restart = start()

使用

break
关键字(并将代码从
start()
直接移动到
while
循环中)是控制此问题的另一种方法。


0
投票

global restart
添加到您的
start
函数中:

def start():
  global restart
  ...

否则,您将在

restart
内创建一个名为
start
的新局部变量,而不是使用全局变量。


0
投票

这里还有另一个错误,

final
条件处理错误

final == ('yes') or ('y') or ("confirm") or ("Yes")

应该是:

final in ('yes','y', "confirm","Yes")

以这种方式写的正确部分立即被评估。 这里有模式详情


-1
投票
##set your restart variable as a bool datatype so it can exist as True or False
restart = bool

def main():
    start()

def start(): #start pos to return to
    #Pizza time!
    ingredients = ['Mozzerrala', '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 additional", 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
    while restart == True:
        if final == ('yes') or ('y') or ("confirm") or ("Yes"):
            print("Thank you for your order, your pizza of ", ingredients, "is being prepared") #will no longer loop as restart is false
            break
        
        elif final == ("no") or ("No") or ("Wrong"):
            print("Sorry, Restarting order")
        else: 
            print('Restarting order')

if __name__ == "__main__":
    main()

您需要在函数之外设置重新启动变量,以便它存在于任何函数都可以访问它的地方 - 您可以使用全局变量,但您应该避免,因为这是一个坏习惯,可能会导致您在更复杂的程序中出现问题。

要小心你的缩进(虽然它可能只是一个粘贴代码的事情,这很好),要注意的一件事是,如果有人输入了不在成分列表中的 rem 内容,你的代码就会中断 - 但我想像您了解到您还没有涵盖这一点,所以您现在可以忽略。

您可以使用break关键字让自己跳出循环,因此您创建了while循环,该循环将反复迭代自身,直到有人给它一个跳出循环的方法,您可以使用break来提供该方法

抱歉,如果我的解释令人困惑,请随时提出任何问题!

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.