2017-03-06 13 views
0

Pythonで定義された名前のエラーメッセージに問題があります。この質問にはかなりの回答があることはわかっていますが、自分の状況に合ったものを見つけることができないようです。次のように私のコードは次のとおりです。別のPython NameError:名前が定義されていません

#Gets the input of the property value from the user and calculates the individual and total revenue 
    def main(): 
     class_A_input = int(input('Please enter the number of Class A seats sold: ')) 
     class_B_input = int(input('Please enter the number of Class B seats sold: ')) 
     class_C_input = int(input('Please enter the number of Class C seats sold: ')) 

    #Declares the cost for each class of ticket 
     class_A_cost = 20 
     class_B_cost = 15 
     class_C_cost = 10 

    #Passes the variable for each ticket class 
     class_A(class_A_input, class_A_cost) 
     class_B(class_B_input, class_B_cost) 
     class_C(class_C_input, class_C_cost) 

    #Calculates the total revenue 
    total_revenue = (class_A_input * class_A_cost) + ,\ 
(class_B_input * class_B_cost) + (class_C_input * class_C_cost) 
    print ('Total tickets revenue is $',format(total_revenue,',d'),sep='') 

    #Calculates the class A revenue 
    def class_A(A_input, A_cost): 
     class_A_revenue = A_input * A_cost 
     print ('The amount of Class A revenue is $',format(class_A_revenue,',d'),sep='') 

    #Repeat definitions for Class B and Class C 

    main() 

私は、Python 3.6.0を実行していると私は、次の名前エラー取得しています:

 total_revenue = (class_A_input * class_A_cost) + ,\ 
(class_B_input * class_B_cost) + (class_C_input * class_C_cost) 
    NameError: name 'class_A_input' is not defined 

を私はそれを使用する前に、私は変数を宣言していないと思います。私は成功していないさまざまなソリューションを試しました。だから私は何が間違っているの?

+3

インデントをチェックするか、ここで修正してください –

+3

発生したコードと同じレベルのインデントを持たないコメントを使用すると、混乱する可能性があります。 –

答えて

1

これは字下げの問題のようです。 total_revenueはグローバルであり、計算にはmain()のローカル変数を使用しようとしています。

P.S.コードの重複を減らすために、関数について学ぶ必要があります。

+0

タイプミスの質問に答える必要はありません。 – TigerhawkT3

+0

@ TigerhawkT3良い点ですが、かなりの空白がある場合は、何かがちょうどタイプミスであるか、OPの基本的な誤解を示しているかどうかは必ずしも明確ではありません。 –

関連する問題