我的第一个python类程序遇到了问题。这应该取三个值,这两个值是用两个不同的函数计算的(一个有一个,一个没有红利)。然后我想在main函数中输出所有结果。如您所见,我在正确打印结果时遇到问题。如果我输入一些将返回薪水,奖金,基数,工作月数和佣金的变量,如果工作月份,我应该收回所有这些变量> 3,如果工作月数没有奖金< 3.它还应该根据另外两个if语句减去或增加一个额外的值。
#calculate income rates with sales, bonus, comission
def income(sales):
if (sales > 1000000):
bonus = 100000
commission = .35
salary = 2000 + sales + bonus + (sales * commission)
elif(sales >= 500001 and sales <= 1000000):
bonus = 5000
commission = .28
salary = 2000 + sales + bonus + (sales * commission)
elif (sales >= 100001 and sales <= 500000):
bonus = 1000
commission = .15
salary = 2000 + sales + bonus + (sales * commission)
elif (sales >= 10000 and sales <= 100000):
bonus = 0
commission = .02
salary = 2000 + sales + bonus + (sales * commission)
else:
bonus = 0
commission = 0
salary = 2000 + sales + bonus + (sales * commission)
return salary
return bonus
return commission
#calculate income rates with sales and commission, no bonus
def calc_no_bonus(sales):
bonus = 0
if (sales > 1000000):
commission = .35
salary = 2000 + sales + (sales * commission)
elif(sales >= 500001 and sales <= 1000000):
commission = .28
salary = 2000 + sales + (sales * commission)
elif (sales >= 100001 and sales <= 500000):
commission = .15
salary = 2000 + sales + (sales * commission)
elif (sales >= 10000 and sales <= 100000):
commission = .02
salary = 2000 + sales + (sales * commission)
else:
commission = 0
salary = 2000 + sales + (sales * commission)
return salary
return commission
def main():
name = input("What is your name? ")
sales = float(input("Input your annual sales: "))
vacation = int(input("How many vacation days have you taken? "))
months = int(input("How many years have you been with the company? Please enter in number of months: "))
base = 2000
salary = 0
bonus= 0
commission = 0
if (months < 3):
salary = calc_no_bonus(sales)
else:
salary = income(sales)
bonus = income(bonus)
commission = income(commission)
#For salespeople who have been with the company for more than 5 years and who have made sales greater than $100,000 an additional bonus of $1000 is added
if (months > 60 and sales > 100000):
salary = income(sales) + 1000
#If a salesperson has taken more than 3 vacation days in a month, their pay gets reduced
#by $200
if (vacation > 3):
salary = income(sales) - 200
print("Your total salary is $", format(salary, ',.2f'), sep='')
print("This consists of: \n your sales of $", format(sales, ',.2f'), sep='')
print("your base of $", base)
print("your bonus of $", bonus)
print("your commission of $", commission)
main()
结果:
What is your name? j
Input your annual sales: 1000000
How many vacation days have you taken? 6
How many years have you been with the company? Please enter in number of months: 2
Your total salary is $1,286,800.00
This consists of:
your sales of $1,000,000.00
your base of $ 2000
your bonus of $ 0
your commission of $ 0
What is your name? j
Input your annual sales: 1000000
How many vacation days have you taken? 8
How many years have you been with the company? Please enter in number of months: 4
Your total salary is $1,286,800.00
This consists of:
your sales of $1,000,000.00
your base of $ 2000
your bonus of $ 2000
your commission of $ 2000
The syntax of salary:
1,000,000 sales
350,000 comission
100,000 bonus
2,000 base
salary should be 1,452,000?
答案 0 :(得分:2)
首先返回退出(这意味着第二次退回不起作用)
要克服这种变化的语法 -
return (salary, bonus, commision)
已编辑 - 这将输出重新作为一个tupple,下面的代码将让您了解如何使用它来获取输出
def income(sales):
salary = sales*0.1
bonus = sales*0.2
commision = sales *0.3
return (salary, bonus, commision)
sample = 1000
income2 = income(sample)
print income2[1]
`
答案 1 :(得分:1)
如果需要返回多个值,则必须在一个语句中返回所有值。然后你就可以解开&#34;通过将返回值分配给多个变量来得到结果。
例如:
def income(sales):
...
return salary, bonus, commission
...
salary, bonus, commission = income(sales)