Python 而循环不是';工作不正常吗?
我正在创建一个数学测验来练习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): ")
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]中时,它更明显地写为
:
。