如何在python上将if语句设置为一次小于或大于? a=int(输入(“输入选项:”) 如果a>3且a3或a
正如您所看到的,我希望它允许“a”小于1且大于3,但我编写它的方式不起作用 您可以以相反的方式链接条件:如何在python上将if语句设置为一次小于或大于? a=int(输入(“输入选项:”) 如果a>3且a3或a,python,python-3.x,Python,Python 3.x,正如您所看到的,我希望它允许“a”小于1且大于3,但我编写它的方式不起作用 您可以以相反的方式链接条件: 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") 如果1您使用了错误的条件 要检查是否满
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")
如果1您使用了错误的条件
要检查是否满足任一条件,请使用或:
if 1 <= a <= 3:
print("Correct choice")
else:
print("Invalid choice")
如果a>3或a<1:
要检查两个条件是否都满足(当然,在这种情况下不可能),您可以使用和只需使用或而不是和。布尔逻辑的胜利@AkshatMahajan哇,修好了,谢谢!数字不能同时大于3和小于1。OP希望检查A是否大于3或小于1,而不是是否介于1和3之间。:)仔细看看。我想你没有注意到我把if子句中的内容放在else子句中,反之亦然,有效地实现了OP的要求,并且可以说使代码更可读。我以这句话结束,它使它更容易谢谢你
if a > 3 or a < 1: