Python循环没有中断

Python循环没有中断,python,loops,while-loop,Python,Loops,While Loop,当我的isQuit变量为“yes”且客户变为False时,我希望整个循环退出。但是,当前循环似乎会重复,直到订单项目位于菜单项目中 你能帮我修改代码,这样当用户想退出时循环就停止了吗 def order(menu): FISH_CHIPS_PRICES = menu menu_items = [] for item in FISH_CHIPS_PRICES: menu_items.append(item.lower()) orders = []

当我的
isQuit
变量为
“yes”
且客户变为
False时,我希望整个循环退出。但是,当前循环似乎会重复,直到
订单项目
位于
菜单项目

你能帮我修改代码,这样当用户想退出时循环就停止了吗

def order(menu):
    FISH_CHIPS_PRICES = menu
    menu_items = []

    for item in FISH_CHIPS_PRICES:
        menu_items.append(item.lower())

    orders = []
    customer = True
    while customer:
        orders.append({})

        for item in FISH_CHIPS_PRICES:
           orders[-1][item] = 0

        while True:
            while True:
                order_item = input("What do you want to buy?")
                if order_item.lower() not in menu_items:
                    print("Item:", order_item, "not available")
                    isQuit = input("Do you want to quit: yes / no:")
                    if isQuit == "yes":
                        customer = False
                else:
                    break

您看到这种行为是因为
isQuit
变量包含两个无限while循环<代码>中断
退出循环只会停止立即封闭的循环

一种可能的解决方案是,当
isQuit
时,立即
返回
,这将退出整个
order()
函数

# ...
isQuit = input("Do you want to quit: yes / no:")
if isQuit == "yes":
    return

另一个可能的解决方案是删除外部无限while循环,因为它似乎没有任何功能。

break
只能中断一层循环。两个嵌套的
while True
做什么?@iBug,我经常有类似
if(condition)if(condition)doSomething()的代码-在继续之前,我想确认
条件
为真:-)好的,谢谢。我认为问题与变量范围有关,变量没有改变。我认为这两种改变都应该进行。我同意。我还想到了一些额外的变化。但是我真的不想重写OP的整个函数:)谢谢你的回答,我只是想打破外部while循环,而不是整个函数,因为还有一些额外的代码。你的评论和其他人帮助我确定了中断只会中断直接循环,所以我必须在内部循环之外的某个阶段中断外部循环。我按照你的建议设法完成了这件事。伟大的很高兴我能帮忙!请考虑接受并投票回答:
# ...
isQuit = input("Do you want to quit: yes / no:")
if isQuit == "yes":
    return