异常块无法捕获错误

问题描述 投票:0回答:1

我正在尝试制定一个选民登记程序。如果未输入正确的输入,我希望它捕获错误,然后要求用户重试。然后显然会重新提示用户输入正确的输入。然而,在测试我的代码后,当我输入除“是”或“否”以外的任何内容时,代码只会在控制台中抛出错误。总的来说,我似乎无法让 try- except 块工作。

如有任何帮助,我们将不胜感激。我以前用Python编写过代码,但我绝对不是专家。仍在打基础

代码 代码第 2 部分

def main():
x = 0
while True:
    try:
        if x == 0:
            register_answer = input("Would you like to register to vote? (y/n): ")
            if register_answer == "y":
                x += 1
            elif register_answer.lower() == "n":
                print("Closing Application")
                break
        if x == 1:
            citizenship = input("Are you a U.S. Citizen? (y/n): ")
            if citizenship == "y":
                x += 1
            else:
                print("Sorry, you must be a U.S. Citizen to vote.\nClosing Application")
                break
        if x == 2:
            age = int(input("How old are you? "))
            if age >= 18 and age <= 120:
                x += 1
            else:
                print("Sorry, you do not meet the minimum age of 18 years old or "
                          "you input an invalid age.\nClosing Application")
                break
        if x == 3:
            first_name = input("What is your first name? ")
            last_name = input("What is your last name? ")
            x += 1
        if x == 4:
            state = input("State of residence: ")
            valid_state = state_finder(state)
            if valid_state == True:
                x += 1
        if x == 5:
            zipcode = input("Zip code: ")
            if len(zipcode) == 5:
                x += 1
    except:
        print("Invalid Input")
    else:
        print(f"Thanks for registering to vote. Here is the information we \n"
              f"received:\nName: {first_name} {last_name}\nAge: {age}\nU.S. Citizen: {citizenship.upper()}"
              f"\nState: {state.upper()}\nZip code: {zipcode}"
              f"\nThanks for trying the Voter Registration Application. "
              f"Your voter registration card should be shipped within 3 weeks.")
        break

我检查了诸如缩进之类的东西,没有发现任何问题,但我已经有一段时间没有用 python 编码了,所以我可能会遗漏一些东西。

python python-3.x exception
1个回答
0
投票

您的异常不是由于错误的输入,而是由于代码到达

else
语句,但没有
first_name
last_name
等值。

如果用户输入的内容与您预期的不同,则输入仍然有效并且不会引发异常!例如,对于您的第一次检查,如果既没有输入 y 也没有输入 n ,则发生的所有情况是它不会增加

x
,因此会跳过所有其他
if x == ?
语句并直接进入
else:
语句无一例外!

要处理这个问题,您可以在您的

else:

中实现它
        else:
            if x == 6:  # check if all questions are answered, only then break the loop
                print(f"Thanks for registering to vote. Here is the information we \n"
                      f"received:\nName: {first_name} {last_name}\nAge: {age}\nU.S. Citizen: {citizenship.upper()}"
                      f"\nState: {state.upper()}\nZip code: {zipcode}"
                      f"\nThanks for trying the Voter Registration Application. "
                      f"Your voter registration card should be shipped within 3 weeks.")
                break
© www.soinside.com 2019 - 2024. All rights reserved.