如何在python中同时检查整数和范围?

时间:2018-06-21 21:39:28

标签: 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

3 个答案:

答案 0 :(得分:2)

  

我无法将两者结合在一起

可以。有很多可能的解决方案,这里是一个:

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

答案 1 :(得分:0)

我不知道我是否理解你的问题...但是也许

if user_inputted_string.isdigit() and 800 <= int(user_inputted_string) <= 1500:
   print("You Need To Put an int between 800,1500 ")

答案 2 :(得分:0)

尝试一下:

old_async = debugger.GetAsync()    
debugger.SetAsync(True)
process.Continue()
debugger.SetAsync(old_async)