“display_results undefined”错误,我无法弄清楚原因。我是编程新手

时间:2016-09-30 06:39:03

标签: python-3.x

这是我的代码,我不知道我能做什么。每次我尝试运行这段代码时,无论我改变了什么,我都会挂在同一个地方:“builtins.NameError:name'hrlyly_pay'未定义”我在代码中标记了它在哪一行;在哪里我尝试定义“display_results”

def main():
    display_message()

    #Get name
    employee_name=input("Enter employee's name: ")

    #Get Sales 
    sales_amount = float(input("Enter the sales amount: "))

    #Get Hours
    hours_worked = int(input("Enter hours worked by this employee: "))

    #Calculate Hourly Pay
    hourly_pay = hours_worked * hourly_pay_rate

    #Calculate Commission
    commission = sales_amount * commission_rate

    #Calculate Gross Pay
    gross_pay = hourly_pay + commission_rate

    #Calculate Witholding 
    witholding = gross_pay * witholding_rate

    #Calculate Net Pay
    net_pay = gross_pay -witholding 
    display_results = (hourly_pay, commission, gross_pay, witholding, net_pay)

def display_message():
    print('This program uses functions to calculate pay, commission, gross')
    print('pay, witholding, and net pay.')

def display_results():
    print('Hourly pay amount is: ' (hourly_pay))   #<-- This is where it errors  
    print('commission amount is: ' (commission))     
    print('Gross pay amount is: ' (gross_pay))    
    print('Witholding amount is:' (witholding))       
    print('Net pay amount is:' (net_pay))


hourly_pay_rate = 7.5
commission_rate = 0.05
witholding_rate = 0.25


main()    
display_results()

1 个答案:

答案 0 :(得分:0)

您正在尝试引用本地范围内的全局变量,该变量将失败。此外,您正在尝试自动将str转换为int。正确的方法如下所示:

解决方案

使用global关键字来引用全局范围变量。还有Refer to Doc关于全局和局部范围变量。

def main():
    global hourly_pay

    display_message()

    #Get name
    employee_name=input("Enter employee's name: ")

    #Get Sales 
    sales_amount = float(input("Enter the sales amount: "))

    #Get Hours
    hours_worked = int(input("Enter hours worked by this employee: "))

    #Calculate Hourly Pay
    hourly_pay = hours_worked * hourly_pay_rate

    #Calculate Commission
    commission = sales_amount * commission_rate

    #Calculate Gross Pay
    gross_pay = hourly_pay + commission_rate

    #Calculate Witholding 
    witholding = gross_pay * witholding_rate

    #Calculate Net Pay
    net_pay = gross_pay -witholding 
    display_results = (hourly_pay, commission, gross_pay, witholding, net_pay)

def display_message():
    print('This program uses functions to calculate pay, commission, gross')
    print('pay, witholding, and net pay.')

def display_results():
    global hourly_pay
    print('Hourly pay amount is: {}'.format(hourly_pay))
    # print('commission amount is: ' (commission))
    # print('Gross pay amount is: ' (gross_pay))
    # print('Witholding amount is:' (witholding))
    # print('Net pay amount is:' (net_pay))


if __name__ == "__main__":
    hourly_pay = 0.00
    commission = 0.00
    gross_pay = 0.00
    witholding = 0.00
    net_pay = 0.00

    hourly_pay_rate = 7.5
    commission_rate = 0.05
    witholding_rate = 0.25

    main()
    display_results()

如果这样可以解决您的问题,请告诉我们。我自己尝试过并返回15.0作为输出。