微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我正在尝试在 python 中进行函数计算,每次运行该模块时,我都会收到一条错误消息,指出“名称'销售奖金未定义

如何解决我正在尝试在 python 中进行函数计算,每次运行该模块时,我都会收到一条错误消息,指出“名称'销售奖金未定义

#带函数的年终奖金计算 #修改提供的计算机程序,使奖金的计算由一个函数完成,该函数的输入数据(其参数)是员工当年的总销售额和员工的服务年限。您的函数必须使用提供的程序代码中使用的相同决策逻辑计算并返回员工当年的总奖金。此函数必须从主程序调用,主程序必须首先提示用户输入当年的总销售额和公司的年数,这些需要用作函数调用的参数。最后,主程序必须向用户报告该函数返回的总奖金的值。执行时,这个新版本的奖金计算程序必须产生与提供的原始代码产生的结果完全相同的结果。 #函数定义:

--- 签名(标题

def yearlyBonus(yearSales,yearsOfService):
    #Calculate monthly average sales 
    monthlyAveSales = yearSales / 12
    #Decide bonus based on monthly sales average
    if monthlyAveSales < 1000:
        salesBonus = 100
    elif monthlyAveSales >= 1000 and monthlyAveSales < 2000:
        salesBonus = 200
    elif monthlyAveSales >= 2000 and monthlyAveSales < 3000:
        salesBonus = 300
    else:
        salesBonus = 400
    return(salesBonus)
#
#
#Main Program 
#Prompt user to enter basic data
yearSales = float(input('Enter the total sales for the year: '))
yearsOfService = int(input('Enter the number of years with the company: '))
#Calculate the yearly bonus by adjusting the sales bonus
#per years of service:
#if years of service is 10 or more,#increase the sales bonus by the appropriate percent
if yearsOfService >= 20:
    yearlyBonus = salesBonus * 1.20
elif yearsOfService >= 10:
    yearlyBonus = salesBonus * 1.10
else:
    yearlyBonus = salesBonus
#Report yearly bonus
print('The bonus for the year is {0:.2f}.'.format(yearlyBonus(yearSales,yearsOfService)))

解决方法

您需要在 if..else 之前在 main 中设置 salesBonus = yearlyBonus(yearSales,yearsOfService)。或者更好地尝试重新编码,因为它有点混乱

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。