📜  python 检查 input() 是否给出错误 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:50.420000             🧑  作者: Mango

代码示例1
try:
    # Note: Python 2.x users should use raw_input, the equivalent of 3.x's input
    age = int(input("Please enter your age: "))
except ValueError:
    print("Sorry, I didn't understand that.")
else:
    if age >= 18: 
        print("You are able to vote in the United States!")
    else:
        print("You are not able to vote in the United States.")