除了 ValueError 在我的代码中不起作用我不知道为什么

except ValueError isnt working in my code i dont know why

我包含了 ValueError 以确保用户输入一个整数,但它指出未绑定的本地错误并指出变量在赋值之前被引用

def bagTotal():
    while True:
        try:
            bagCount = int(input("Now we just need the number of bags you wish to take with you: "))          
        except ValueError:
            print("Please input a number")
            print("")
        if (bagCount <= 2):
            print("As stated before the first bag is free")
            print(name,",your total is","$%>2F"%ticket)
            print("")
            print("")
            restart()
        else:
            bagTotal = (bagCount - 1) * bagFee
            ticketTotal = bagTotal + ticketamount
            print(name,", your new total is","$%.2f"%ticketTotal)
            print("")
            print("")
            restart()

如果您得到一个 ValueError,程序将继续并且 运行 您的 if 语句等,然后再进入 while 循环的下一个循环。如果您没有收到错误,您只希望该代码为 运行,因此您的代码应如下所示:

def bagTotal():
    while True:
        try:
            bagCount = int(input("Now we just need the number of bags you wish to take with you: "))
            if (bagCount <= 2):
                print("As stated before the first bag is free")
                print(name,",your total is","$%>2F"%ticket)
                print("")
                print("")
                restart()
            else:
                bagTotal = (bagCount - 1) * bagFee
                ticketTotal = bagTotal + ticketamount
                print(name,", your new total is","$%.2f"%ticketTotal)
                print("")
                print("")
                restart()          
        except ValueError:
            print("Please input a number")
            print("")

这是修改后的代码,请检查,我已经注释了几行并且还使用了一些变量 我在您的代码中看到它可以在我的系统中运行。

def bagTotal():
    # The below 4 initializations are just
    # to stop errors in your problem, you use your own values
    bagFee = 100      
    ticketamount = 60 
    name = 'Rampel Jons'
    ticket = 70

    while True:
        try:
            bagCount = int(input("Now we just need the number of bags you wish to take with you: "))          
            if (bagCount <= 2):
                print("As stated before the first bag is free")
                print(name,",your total is","$%>2F"%ticket)
                print("")
                print("")
                restart() 
            else:
                bagTotal = (bagCount - 1) * bagFee
                ticketTotal = bagTotal + ticketamount
                print(name,", your new total is","$%.2f"%ticketTotal)
                print("")
                print("")
                restart() 
        except ValueError:
            print("Please input a number")
            print("")

# call
bagTotal()

# Now we just need the number of bags you wish to take with you: 5
# Rampel Jons , your new total is 0.00


# Now we just need the number of bags you wish to take with you: 7
# Rampel Jons , your new total is 0.00


# Now we just need the number of bags you wish to take with you: 9
# Rampel Jons , your new total is 0.00


# Now we just need the number of bags you wish to take with you: fjjfjf
# Please input a number

# Now we just need the number of bags you wish to take with you: