猿问

如何在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


手掌心
浏览 189回答 3
3回答

红糖糍粑

我不能将两者结合在一起你当然可以。有很多可能的解决方案,这是一个:#UNTESTEDbase_pay = Nonewhile True:&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; base_pay = int(input(">>> "))&nbsp; &nbsp; &nbsp; &nbsp; if 800 <= base_pay <= 1500:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; print("Please enter a value between 800 and 1500")&nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; print("Numbers only please")

慕的地10843

试试这个:if not(base_pay <= 1500 and base_pay >= 800):&nbsp; &nbsp; print("Please enter a value between 800 and 1500")&nbsp; &nbsp; continue

至尊宝的传说

我不知道我是否理解您的问题...但是也许if user_inputted_string.isdigit() and 800 <= int(user_inputted_string) <= 1500:&nbsp; &nbsp;print("You Need To Put an int between 800,1500 ")
随时随地看视频慕课网APP

相关分类

Python
我要回答