base_pay = Nonewhile 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 回答

紅糖糍粑
TA貢獻1815條經驗 獲得超6個贊
我不能將兩者結合在一起
你當然可以。有很多可能的解決方案,這是一個:
#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")

慕的地10843
TA貢獻1785條經驗 獲得超8個贊
試試這個:
if not(base_pay <= 1500 and base_pay >= 800):
print("Please enter a value between 800 and 1500")
continue

至尊寶的傳說
TA貢獻1789條經驗 獲得超10個贊
我不知道我是否理解您的問題...但是也許
if user_inputted_string.isdigit() and 800 <= int(user_inputted_string) <= 1500:
print("You Need To Put an int between 800,1500 ")
添加回答
舉報
0/150
提交
取消