#带函数的年终奖金计算 #修改提供的计算机程序,以便通过输入数据(其参数)为员工当年总销售额和服务年限的函数计算奖金。您的函数必须使用提供的程序代码中使用的相同决策逻辑计算并返回员工当年的总奖金。必须从主程序调用此函数,主程序必须首先提示用户输入该年的总销售额和公司的年数,这需要用作函数调用的参数。最后,主程序必须向用户报告函数返回的奖金总额。执行时,新版本的奖金计算程序必须产生与原始代码完全相同的结果。 #功能定义:
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)
。或者最好尝试重新编码,因为它有点混乱相关问题 更多 >
编程相关推荐