我在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
我不認爲我聲明變量之前我用它。我嘗試了各種不同的解決方案,但都沒有成功。那麼我做錯了什麼?
檢查縮進或在此處修復它 –
使用與它們出現的代碼不同的縮進級別的註釋是讓自己感到困惑的好方法。 –