📜  python 如何使用 while 循环来处理无效输入 - TypeScript 代码示例

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

代码示例1
while True:
    try:
        age = int(input("Please enter your age: "))
    except ValueError:
        print("Sorry, I didn't understand that.")
        continue

    if age < 0:
        print("Sorry, your response must not be negative.")
        continue
    else:
        #age was successfully parsed, and we're happy with its value.
        #we're ready to exit the loop.
        break
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.")