更新しました:
このコードは機能します:
# 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消費税見積もりプログラムで動作するように変更することにした素晴らしいコードのセットが提供されました。
1つは、よくわからないブレークアウトループエラーが発生していることです。私は検索して意味を理解しようとしましたが、コードが以前に機能したことは知っています。また、私が最初から作成した税コードプログラム:)は、ユーザーが「終了」するまでループで多くの入力を送信する空想機能を追加しようとする前に機能しました。
コードは次のとおりです。
# 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()
PS試してみた後、エラーステートメントが必要であると調査したため、KeyErrorのみを追加しました。私はまだ初心者なので、自分でプログラムを作成して「Python fortheAbsoluteBeginner」を読んでいます。
アップデート:
インデントを修正しましたが、次のトレースバックエラーが発生します。
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())
小計が定義される前に、定義された小計を使用する他の定義(税および合計)が定義されているためですか?定義した小計の下に移動してみましたが、まだ機能しませんでした。
アドバイスありがとうございます。
一番、
スティーブン