【发布时间】: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