2017-03-06 123 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的根本性誤解。 –