Python 3.x上基本脚本中的无限循环

时间:2019-02-08 08:31:21

标签: python-3.x

我开始在python 3.x中进行编码,在我的第一个脚本中,我执行了一个循环,而用户键入除'b'或'B'以外的任何内容时,他都会停留在循环中,但实际上当我键入'b'或' B'我留在循环中,我不知道为什么。

抱歉,我的英语是青蛙,你知道x)

最好的问候

我尝试使用raw_input纠正此问题,但似乎在python 3.x中未定义raw_input()。

    ipt2 = input ('tapez b pour quitter')

    while ipt2 != 'b' or ipt2 != 'B':
        print(get_random_quote(quotes))
        ipt2 = input ('tapez b pour quitter')
        print(ipt2)

当我输入B时,我会停留在循环中,我认为我必须退出循环,不是吗?

最后一行是在这里查看ipt2的值是什么。

1 个答案:

答案 0 :(得分:0)

问题是您正在使用CREATE newFile OPEN newFile ACCESS newFile CLOSE_NOWRITE:CLOSE newFile 进行检查,但是在这种情况下,您需要使用or

and

逻辑如下:

第一个示例

ipt2 = A

在行ipt2 = input ('tapez b pour quitter') while ipt2 != 'b' and ipt2 != 'B': print(get_random_quote(quotes)) ipt2 = input ('tapez b pour quitter') print(ipt2) 中,我们检查了以下内容:

1)ipt2不是'b'(ipt2是A,所以正确或while ipt2 != 'b' and ipt2 != 'B':

2)ipt2不是'B'(ipt2是A,因此正确或True

两个条件均为True,因此循环继续进行。

第二个示例

ipt2 = B

现在我们再次检查:

1)ipt2不是'b'(ipt2是B,所以是正确的或True

2)ipt2不是'B'(ipt2是B,所以不正确或True

一个条件为False,另一个条件为True,因此循环继续进行,因为我们正在检查 conidition1 OR conidition2 是True。如果Falseipt2,那么ipt2不是'B',反之亦然,一个结果将始终为True,并且循环将继续。

相关问题