简体   繁体   English

我的变量已定义,但是python表示不是吗?

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

I keep getting an error telling me that the name hourly_pay is not defined, but I have it defined inside the main function. 我不断收到错误消息,告诉我名称hourly_pay尚未定义,但是我已在main函数中定义了它。

I'm a beginner as I've just started class but to me it looks like it should be working: 我是一个初学者,因为我刚刚开始上课,但对我来说似乎应该可以工作:

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

In python (in contrast to JavaScript), variables are locally scoped by default. 在python中(与JavaScript相反),默认情况下变量在本地范围内。 This means that the variables are only accessible inside the function they are defined in. This behaviour can be overridden, but usually you do not want that . 这意味着变量只能在定义它们的函数内部访问。可以重写此行为,但通常不希望这样做

To illustrate the difference, take a look at this python transcript: 为了说明不同之处,请看以下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

As you can see, even though var1 is assigned to in the foo() function, the value of the var1 name does not change in the global scope. 如您所见,即使在foo()函数中将var1分配给它, var1名称的值在全局范围内也不会改变。 If we had not defined var1 globally at all, the two print(var1) calls outside foo() would fail with a NameError, just like your code does. 如果我们根本没有全局定义var1 ,则foo()之外的两个print(var1)调用将失败,并出现NameError,就像您的代码一样。

The ultimate solution to your problem is to either handle output in the main() function, or pass the values to the display_results() function (the latter is generally preferred, keep logic and output separated): 解决您问题的最终方法是在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 (emphasis mine): 官方的Python教程还对函数作用域 (重点是我的话) 说了几句话

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. 因此,尽管可以引用全局变量,但不能在函数内直接为其赋值 (除非在global语句中命名)。

hourly_paying is defined in main() and it stays in main's scope. hourly_payingmain()定义,并且处于main的范围内。 You need to pass it to display_results and modify display_results to accept all the values that you need. 您需要将其传递给display_results并修改display_results以接受所需的所有值。 For example: 例如:

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....')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM