除ValueError之外的Python:

时间:2015-11-29 01:02:53

标签: python syntax-error

user_input = input('How old are you?: ')
if user_input >= 18:
   print('You are an adult')
elif user_input < 18:
     print('You are quite young')
except ValueError:
    print('Please type in an integer') 

它说除了

之外的语法错误

请帮忙。

1 个答案:

答案 0 :(得分:1)

except不与if/elif/else一起使用。相反,它与try一样,如下所示:

user_input = input('How old are you?: ')
try:
    age = int(user_input) #this can raise ValueError
    if age >= 18:
        print('You are an adult')
    elif age < 18:
        print('You are quite young')
except ValueError:
    print('Please type in an integer')