【问题标题】:except ValueError isnt working in my code i dont know why除了 ValueError 在我的代码中不起作用我不知道为什么
【发布时间】:2019-05-21 14:14:33
【问题描述】:

我包含 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()

【问题讨论】:

  • 请发布一些代码并指出它是什么语言。就你的问题而言,我不知道你在讨论什么。
  • 我在使用 python 时表现不佳,我不确定如何发布我之前尝试过的代码,但结果很奇怪
  • 您需要选择您的代码并按下工具栏上的 {} 按钮,或者手动将所有内容缩进四个空格。尝试发布,如果不完美,希望有人会整理它。
  • 好的,谢谢刚刚发布的代码
  • 我添加了[python] 标签,这样代码就会被着色。一个快速的解决方法是将continue 添加为except 块的最后一行,以便您跳转到循环的开头。

标签: python python-3.x valueerror


【解决方案1】:

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

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 $460.00


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


# Now we just need the number of bags you wish to take with you: 9
# Rampel Jons , your new total is $860.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:

【讨论】:

  • 对不起,这只是我的代码的一部分,还有另一个名为 restart() 的 mod,它让用户可以选择为不同的票输入更多的包
【解决方案2】:

如果您遇到 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("")

【讨论】:

    猜你喜欢
    • 2020-09-19
    • 2014-01-17
    • 1970-01-01
    • 2022-01-22
    • 2021-10-19
    • 2020-02-05
    • 2021-03-11
    • 1970-01-01
    • 2019-01-12
    相关资源
    最近更新 更多