更新:
本守则有效:
# North Carolina Sales Tax Estimator
# Estimates the Amount of Tax You Should Pay in North Carolina
# Defines the Current 2012 Tax Rate
nctaxrate = 0.07
# Defines the tax variable by multipling subtotal by the current nc tax rate
# tax = subtotal * nctaxrate
# Defines the total variable by adding the tax variable to the subtotal variable
# total = subtotal + tax
# Defines the Subtotal from an Input of the User's Purchase Amount
def main():
print("\t\t\tThis is the NC Sales Tax Estimator")
print("\t\t Input Your Total Purchases Below\n")
while True:
subtotal = float(input("Enter the total price of your purchases:\t$").strip())
if subtotal == -1: break
tax = subtotal * nctaxrate
total = subtotal + tax
print("\tSUBTOTAL: $", subtotal)
print("\t TAX: $", tax)
print("\t TOTAL: $", total)
# if this script is called directly by Python, run the main() function
# (if it is loaded as a module by another Python script, don't)
if __name__=="__main__":
main()
这是原始问题:
所以我正在学习Python并在昨天问了一个前面的问题,并提供了一组很棒的代码,我决定修改它以使用我想要创建的NC销售税估算程序。
一件事是我遇到了一个我不太明白的突破循环错误。我已经搜索并尝试理解其含义,但我知道代码之前有效。此外,我从头开始创建的税务代码程序在尝试添加奇特的功能之前,在循环中提交许多输入,直到用户想要“退出”为止。
以下是代码:
# North Carolina Sales Tax Estimator
# Estimates the Amount of Tax You Should Pay in North Carolina
# Defines the Current 2012 Tax Rate
nctaxrate = 0.07
# Defines the tax variable by multipling subtotal by the current nc tax rate
tax = subtotal * nctaxrate
# Defines the total variable by adding the tax variable to the subtotal variable
total = subtotal + tax
# Defines the Subtotal from an Input of the User's Purchase Amount
def main():
print("\t\t\tThis is the NC Sales Tax Estimator")
print("\t\t Input Your Total Purchases Below")
while True:
subtotal = float(input("Enter the total price of your purchases (or 'exit' to quit) :\$").strip())
if subtotal.lower()=='exit':
break
try:
subtotal = int(subtotal)
except ValueError:
print("That wasn't a number!")
try:
print("\tSUBTOTAL: $", subtotal)
print("\t TAX: $", tax)
print("\t TOTAL: $", total)
except KeyError:
print("")
# if this script is called directly by Python, run the main() function
# (if it is loaded as a module by another Python script, don't)
if __name__=="__main__":
main()
P.S。我只添加了KeyError,因为我研究过你在尝试后必须有一个错误语句。我只是一个初学者,所以我试图自己创建程序并阅读“Python for Absolute Beginner”。
更新:
我修复了缩进,但现在我收到以下回溯错误:
Traceback (most recent call last):
File "C:/LearningPython/taxestimator.py", line 30, in <module>
tax = subtotal * nctaxrate
NameError: name 'subtotal' is not defined
我以为我在输入中定义了它。
subtotal = float(input("Enter the total price of your purchases (or 'exit' to quit) :\$").strip())
是否因为在定义小计之前定义了使用定义小计的其他定义(税和总)?我尝试将它们移动到定义的小计下面,但它仍然不起作用。
感谢任何建议。
最佳,
史蒂芬
答案 0 :(得分:1)
你遇到的主要问题是Python需要空格来定义方法。如果没有这个,那么语句将不会按预期运行 - 例如:
while True:
subtotal = float(input("Enter the total price of your purchases (or 'exit' to quit) :\$").strip())
if subtotal.lower()=='exit':
break
不会在有效输入的情况下突破循环(如果你在那里放了一个字符串,那就是另一回事)。此外,如果所有内容都在main()
的范围内,则每个语句都需要一个级别的缩进(如果您愿意,则需要四行空格)。目前,您的while
不会在main()
范围内运行。
此外,在实际为其赋值之前,请参考subtotal
。由于subtotal
未正确初始化,因此您无法使用它。
您希望重写代码,以便在定义tax
之后定义 {em} <。}
total
最后,如果subtotal
,while True:
subtotal = float(input("Enter the total price of your purchases (or 'exit' to quit) :\$").strip())
if subtotal.lower()=='exit':
break
tax = subtotal * nctaxrate
total = subtotal + tax
和subtotal
被正确定义(在上述之后,它们将是),则不需要超级{{1}当你想打印出值时,语句。
答案 1 :(得分:0)
if subtotal.lower()=='exit':
break
可能需要缩进...除非在输入问题时这只是一个拼写错误