到目前为止,这是我的代码:
# This program will determine the appropriate bonus to be given to both the store
# and the employess.
# Lab 3-4
# The main function
def main():
monthlySales = getSales() # Call to get sales
salesIncrease = getIncrease() # Call to get sales increase
storeAmount = storeBonus(monthlySales) # Call to get the store bonus
empAmount = empBonus(monthlySales) # Call to get the employee bonus
printBonus (storeAmount, empAmount) # Call to print amounts
# This function gets the monthly sales
def getSales():
monthlySales = input('Enter the monthly sales $')
monthlySales = float(monthlySales)
return monthlySales
# This function gets the percent of increase in sales
def getIncrease():
salesIncrease = input('Enter percent of sales increase. For example, 4% should be entered as 4: ')
salesIncrease = float(salesIncrease)
salesIncrease = salesIncrease / 100
return salesIncrease
# This function determines storeAmount bonus
def storeBonus(monthlySales):
if monthlySales >=110000:
storeAmount = 6000
elif monthlySales >=100000:
storeAmount = 5000
elif monthlySales >=90000:
storeAmount = 4000
elif monthlySales >=80000:
storeAmount = 3000
else:
storeAmount = 0
return storeAmount
# This function determines empAmount bonus
def empBonus(salesIncrease):
if salesIncrease >= .05:
empAmount = 75
elif salesIncrease >= .04:
empAmount = 50
elif salesIncrease >= .03:
empAmount = 40
else:
empAmount = 0
return empAmount
# This function prints the bonus information
def printBonus(storeAmount, empAmount):
print('The store bonus is $', storeAmount)
print('The employee bonus is $', empAmount)
if storeAmount == 6000 and empAmount == 75:
print('Congrats! You have reached the highest bonus amounts possible!')
# Calls main
main()
现在为了测试这段代码,我给出了以下几种情况:
1)monthlySales = 120500 salesIncrease = 5
2)monthlySales = 93400 salesIncrease = 5
3)monthlySales = 75000 salesIncrease = 1.5
4)monthlySales = 82000 salesIncrease = 3.6
5)monthlySales = 125000 salesIncrease = 4.5
现在一切都很好,直到3-5。 “员工奖金额”不应该是75美元,不过这就是出来的。关于如何解决这个问题的任何想法?任何人都看到我的代码中没有看到任何错误?
*编辑:每个输入值应输出的代码如下:
1)$ 6000 $ 75
2)$ 4000 $ 75
3)$ 0 $ 0
4)$ 3000 $ 40
5)$ 6000 $ 50
答案 0 :(得分:6)
很难说出你的问题是什么,因为你的问题没有解释你想要做什么,但我会猜测你已经关闭了100倍:
def empBonus(salesIncrease):
if salesIncrease >= 5:
empAmount = 75
elif salesIncrease >= 4:
empAmount = 50
elif salesIncrease >= 3:
empAmount = 40
else:
empAmount = 0
return empAmount
要使用您的第三个示例,您正在检查1.5 >= 0.05
当然是True
,因此empAmount
设置为75
。