简体   繁体   English

如何在Python上一次将if语句设置为小于和大于?

[英]How to set an if statement to less than and greater than at once on python?

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")

As you can see i'd like it to allow "a" to be less than 1 and greater than 3, but the way I wrote it does not work. 如您所见,我希望它允许“ a”小于1并大于3,但是我编写它的方式不起作用。

You're using the wrong conditional. 您使用了错误的条件。

To check if either condition is satisfied, use or : 要检查是否满足任何条件,请使用or

if a > 3 or a < 1:

To check if both conditions are satisfied (never possible in this case, of course), you use and . 要检查两个条件是否都满足(当然,在这种情况下永远不可能),请使用and

You can chain the conditions in the opposite way: 您可以用相反的方式链接条件:

if 1 <= a <= 3:
    print("Correct choice")
else:
    print("Invalid choice")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM