[英]How do I check for integer and range at the same time in python?
base_pay = None
while True:
try:
base_pay = int(input(">>> "))
break
except ValueError:
print("Numbers only please")
我如何检查800到1500之间的范围? 我知道我可以使用if
,但是不能将两者结合在一起:
if not (800 <= base_pay <= 1500):
print("Please enter a value between 800 and 1500")
continue
我不能将两者结合在一起
你当然可以。 有很多可能的解决方案,这是一个:
#UNTESTED
base_pay = None
while True:
try:
base_pay = int(input(">>> "))
if 800 <= base_pay <= 1500:
break
print("Please enter a value between 800 and 1500")
except ValueError:
print("Numbers only please")
我不知道我是否理解您的问题...但是也许
if user_inputted_string.isdigit() and 800 <= int(user_inputted_string) <= 1500:
print("You Need To Put an int between 800,1500 ")
尝试这个:
if not(base_pay <= 1500 and base_pay >= 800):
print("Please enter a value between 800 and 1500")
continue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.