另一个Python NameError:name未定义

时间:2017-03-06 00:34:05

标签: python

我遇到Python中定义的名称错误消息的问题。我知道这个问题有很多回复,但我似乎无法找到适合我情况的回答。我的代码如下:

    #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

我不认为我在使用它之前声明了变量。我尝试了各种不同的解决方案但没有成功。那么我做错了什么?

1 个答案:

答案 0 :(得分:1)

这看起来像是缩进问题。 total_revenue是全局的,并尝试在计算中使用main()中的局部变量。

P.S。您应该了解函数,以帮助您减少代码中的重复。