无法从while循环中爆发

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

我是python的初学者,这是我遇到的代码。所以问题是当我按0时循环不会中断

while True:
idiot = input('Continue Y/N?: ')
idiot = idiot.upper()
if idiot == ('Y'):
    print('Great')
if idiot == ('N'):
    print('okey')
if idiot == 0:
    print('exit')
    break
python-3.x while-loop break
1个回答
0
投票

我总是将while循环与变量一起使用。在您的情况下,True从不更改为False,这是循环结束的时间。

这是我的建议:

TrueOrFalse = True
while TrueOrFalse:
    idiot = input('Continue Y/N?: ')
    idiot = idiot.upper()
    if idiot == ('Y'):
        print('Great')
    if idiot == ('N'):
        print('okey')
    if idiot == '0':
        TrueOrFalse = False
        print('exit')

另外,您需要将最后一个if子句更改为if str(idiot) == '0',因为input()总是返回str

另外一件事:我知道这只是一个例子,但

© www.soinside.com 2019 - 2024. All rights reserved.