python - 如何在 python 上同时将 if 语句设置为小于和大于?

标签 python python-3.x

a = int(input("Enter choice: "))
if a > 3 and a < 1:  #the issue is here how can i rewrite it to allow this?
    print("Invalid choice")
else:
    print("Correct choice")

如您所见,我希望它允许“a”小于 1 且大于 3,但我的写法不起作用。

最佳答案

你使用了错误的条件。

要检查是否满足任一条件,请使用:

if a > 3 or a < 1:

要检查是否满足两个条件(当然,在这种情况下永远不可能),您可以使用and

关于python - 如何在 python 上同时将 if 语句设置为小于和大于?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54918756/

相关文章:

python - 列中的连接值取决于特定条件

javascript - 使用 javascript (jquery) 在 django 中重定向表单提交

python - PySerial 客户端无法写入数据

python - 使用 python 3.5 对 sympy 1.0 中的矩阵项求和

python - 如何将列表的元素作为变量返回?

python - ChromeOption '--safebrowsing-disable-download-protection' 不会禁用 Chrome 版本 67.x 中的下载警告

crash - 在python 3 idle中使用特殊字符^或¨使程序崩溃

python - 更改数据框中多个位置的最快方法

python - 找不到文件错误 : [WinError 2] The system can't find the specified file

python - 同时 pickle 类变量和实例变量?