Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 而循环不是';工作不正常吗?_Python_While Loop - Fatal编程技术网

Python 而循环不是';工作不正常吗?

Python 而循环不是';工作不正常吗?,python,while-loop,Python,While Loop,我正在创建一个数学测验来练习python编程,我遇到了一个问题。我试图创建一个while循环,将其用作验证,以确保用户已输入1、2或3。当我使用这个循环时,它只是继续循环而不退出 cl = int(input("Please enter your class (1, 2 or, 3): ")) while cl != 1 or cl != 2 or cl != 3 : cl = int(input("Please enter your class (1, 2 or, 3): ")

我正在创建一个数学测验来练习python编程,我遇到了一个问题。我试图创建一个
while
循环,将其用作验证,以确保用户已输入1、2或3。当我使用这个循环时,它只是继续循环而不退出

cl = int(input("Please enter your class (1, 2 or, 3): "))  

while cl != 1 or cl != 2 or cl != 3 : 
    cl = int(input("Please enter your class (1, 2 or, 3): "))
我有代码后保存其他信息到一个文本文件(我知道csv文件可能更容易),它不会继续这个代码


我如何解决这个问题,或者是否有更好的方法来做我正在尝试的事情
cl
是一个变量,将以整数的形式作为用户的输入。整数将用于在if和elif语句之间进行选择。

此条件始终为真!要使其正常工作,您需要使用

while cl != 1 and cl != 2 and cl != 3: 
              ^^^         ^^^
以基本情况为例:
c1!=1或c1!=2
并查看逻辑如何应用于您的
while
条件和我建议的条件:

c1       c1 != 1   c1 != 2   (c1 != 1 or c1 != 2) (c1 != 1 and c1 != 2)
1         false     true             true                  false
2         true      false            true                  false
!1,2      true      true             true                  true

当cl不在[1,2,3]中时,它更明显地写为