我的变量已定义,但 python 却说没有?

My variable is defined but python is saying it isn't?

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

我是初学者,因为我刚开始 class 但对我来说它看起来应该可以工作:

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()

hourly_paying 是在 main() 中定义的,它保留在 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....')

在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

如您所见,即使在 foo() 函数中将 var1 赋值, var1 名称的值在全局范围内也不会改变。如果我们根本没有全局定义 var1foo() 之外的两个 print(var1) 调用将失败并出现 NameError,就像您的代码一样。

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

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(强调我的):

More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the local symbol tables of enclosing functions, then in the global symbol table, and finally in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.