0

更新しました:

このコードは機能します:

# 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())

小計が定義される前に、定義された小計を使用する他の定義(税および合計)が定義されているためですか?定義した小計の下に移動してみましたが、まだ機能しませんでした。

アドバイスありがとうございます。

一番、

スティーブン

4

2 に答える 2

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()、すべてのステートメントには1レベルのインデント(または、必要に応じて4行の空白)が必要です。現状では、whileのスコープでは実行されませんmain()

また、subtotal実際に値を与える前に参照します。正しく初期化されていないためsubtotal、使用する値がありません。

を定義した後にtaxtotalが定義されるようにコードを書き直したいと思うでしょう。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およびtotaltax適切に定義されている場合(上記の後で、それらは次のようになります)、try...except値を出力するときに余分なステートメントは必要ありません。

于 2012-05-31T17:55:23.697 に答える
0
if subtotal.lower()=='exit':
    break

おそらくインデントする必要があります...それが質問を入力するときの単なるタイプミスでない限り

于 2012-05-31T17:40:25.053 に答える