Python 3.x 错误检查:比如检查负数,或者输入非数字。

Python 3.x 错误检查:比如检查负数,或者输入非数字。,python-3.x,exception,error-checking,Python 3.x,Exception,Error Checking,我是新手,只是不知道应该如何/在哪里使用try和,除非用户输入负数或字符串。可以这样尝试: weight=input('What is your weight?') age=input('What is your age?') w=int(weight) a=int(weight) if (a<=10 and w<80): print('This person needs to ride the black roller coaster') if (a<=10 and 80

我是新手,只是不知道应该如何/在哪里使用try和,除非用户输入负数或字符串。

可以这样尝试:

weight=input('What is your weight?')
age=input('What is your age?')
w=int(weight)
a=int(weight)
if (a<=10 and w<80):
  print('This person needs to ride the black roller coaster')
if (a<=10 and 80<=w<=200):
  print('This person needs to ride the green roller coaster')
if (a<=10 and w>200):
  print('This person needs to ride the yellow roller coaster')
if (10<a<=20 and w<80):
  print('This person needs to ride the silver roller coaster')
if (10<a<=20 and 80<=w<=200):
  print('This person needs to ride the red roller coaster')
if (10<a<=20 and w>200):
  print('This person needs to ride the purple roller coaster')
else:
  print('This person needs to ride the pink roller coaster')

理想的结果是什么?如果用户输入是在什么情况下,则仅显示一条错误消息,如“Entry必须是大于0的数字”?
def checkValue():
while True:
    try:
        global a, w
        a = int(input("What's your age? ").strip())
        w = int(input("What's your weight? ").strip())
    except ValueError as e:
        print("This is not a number. Try again.")
        continue
    if a <= 0 and w <= 0:
        print("Enter value lager than 0.")
        continue
    else:
        break
    return a, w

checkValue ()