我的变量是定义的,但python说它不是吗?

时间:2016-09-08 14:36:42

标签: python variables scope nameerror

我一直收到错误,告诉我名称hourly_pay未定义,但我在main函数中定义了它。

我是初学者,因为我刚开始上课,但对我来说看起来应该有效:

commission_pay_amount = .05
income_taxes = .25
Pay_per_hour = 7.50

def main():
    display_message()

    hourly_pay = float(input('Please enter amount of hours worked: '))

    commission_pay = hourly_pay * commission_pay_amount
    gross_pay = hourly_pay + commission_pay 
    witholding_amount = gross_pay * income_taxes  
    hourly_paying = Pay_per_hour * hourly_pay
    net_pay = gross_pay - witholding_amount

    display_results()

def display_message():
    print('This program is used to calculate')
    print('the hourly pay, commission amount,')
    print('the gross pay, the withholding amount,')
    print('and the net pay amount')
    print()

def display_results():
    print('The hourly pay is $', format(hourly_pay, ',.2f'))
    print('The commission amount is $', format(commission_pay, ',.2f'))
    print('The gross pay is $', format(gross_pay, ',.2f'))
    print('The witholding amount is $', format(witholding_amount, ',.2f'))
    print('The net pay is $', format(net_pay, ',.2f'))

main()

2 个答案:

答案 0 :(得分:3)

在python中(与JavaScript相反),默认情况下变量是本地作用域。这意味着变量只能在它们定义的函数内部访问。这种行为可以被覆盖,但通常是you do not want that

为了说明差异,请看一下这个python成绩单:

>>> var1 = "this is global"
>>> def foo():
...   var1 = "this is local"
...   print(var1)
... 
>>> print(var1)
this is global
>>> foo()
this is local
>>> print(var1)
this is global

如您所见,即使在var1函数中分配了foo()var1名称的值也不会在全局范围内更改。如果我们根本没有全局定义var1,则print(var1)之外的两个foo()调用会因NameError而失败,就像您的代码一样。

问题的最终解决方案是在main()函数中处理输出,或者将值传递给display_results()函数(后者通常是首选,保持逻辑和输出分开):< / p>

def main():
    display_message()
    hourly_pay = float(input('Please enter amount of hours worked: '))
    commission_pay = hourly_pay * commission_pay_amount
    gross_pay = hourly_pay + commission_pay 
    witholding_amount = gross_pay * income_taxes  
    hourly_paying = Pay_per_hour * hourly_pay
    net_pay = gross_pay - witholding_amount

    display_results(hourly_pay, commission_pay, gross_pay, 
                    withholding_amount, net_pay)

def display_message():
    print('This program is used to calculate')
    print('the hourly pay, commission amount,')
    print('the gross pay, the withholding amount,')
    print('and the net pay amount')
    print()

def display_results(hourly_pay, commission_pay, gross_pay, 
                    withholding_amount, net_pay):
    print('The hourly pay is $', format(hourly_paying, ',.2f'))
    print('The commission amount is $', format(commission_pay, ',.2f'))
    print('The gross pay is $', format(gross_pay, ',.2f'))
    print('The witholding amount is $', format(witholding_amount, ',.2f'))
    print('The net pay is $', format(net_pay, ',.2f'))

The official Python tutorial also has a few words on function scopes(强调我的):

  

更确切地说,函数中的所有变量赋值都将值存储在本地符号表中;而变量引用首先在本地符号表中查找,然后在封闭函数的本地符号表中查找,然后在全局符号表中查找,最后在内置名称表中查找。因此,全局变量不能直接在函数中赋值(除非在global语句中命名),尽管它们可能被引用。

答案 1 :(得分:0)

hourly_payingmain()中定义,并且保留在main的范围内。您需要将其传递给display_results并修改display_results以接受您需要的所有值。例如:

commission_pay_amount = .05
income_taxes = .25
Pay_per_hour = 7.50


def main():

    display_message()

    hourly_pay = float(input('Please enter amount of hours worked: '))


    commission_pay = hourly_pay * commission_pay_amount
    gross_pay = hourly_pay + commission_pay
    witholding_amount = gross_pay * income_taxes
    hourly_paying = Pay_per_hour * hourly_pay
    net_pay = gross_pay - witholding_amount

    display_results(hourly_paying,commission_pay,gross_pay,witholding_amount,net_pay)

def display_message():
    print('This program is used to calculate')
    print('the hourly pay, commission amount,')
    print('the gross pay, the withholding amount,')
    print('and the net pay amount')
    print()

def display_results(hourly_paying,commission_pay,gross_pay,witholding_amount,net_pay):
    print('The hourly pay is $', format(hourly_paying, ',.2f'))
    print('The commission amount is $', format(commission_pay, ',.2f'))
    print('The gross pay is $', format(gross_pay, ',.2f'))
    print('The witholding amount is $', format(witholding_amount, ',.2f'))
    print('The net pay is $', format(net_pay, ',.2f'))

main()

input ('Press ENTER to continue....')