2015-06-20 48 views
-2

我遇到了一个python类的第一个程序问题。这应该取三个值,这两个值用两种不同的函数计算(一个和一个没有奖励)。然后我想在主函数中输出所有的结果。正如您所看到的,我在正确打印结果时遇到问题。如果我输入一些将返回工资,奖金,基数,工作月数和佣金的变量,如果月工作> 3,我应该得到所有这些变量,如果月工作时没有奖金< 3.它也应该减去或增加额外基于其他两个if语句的值。Python程序不会返回值奖金

#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? 

回答

1

如果需要返回多个值,你必须返回他们都在一个单独的语句。然后,您可以通过将返回值分配给多个变量来“解压缩”结果。

例如:

def income(sales): 
    ... 
    return salary, bonus, commission 
... 
salary, bonus, commission = income(sales) 
+0

当我试图改变我的返回语句,就像我得到这个错误:ypeError:不支持的操作数类型为 - :'tuple'和'int' –

+0

现在结果关闭: –

+0

你叫什么名字? julie 输入您的年销售额:1000000 您已经度过了多少个假期? 5 你在公司工作多少年了?请在号码中输入月:5 您的总薪水为$ 1,287,000.00 这包括: 您的$ 1,000,000.00 销售的$ 2000 基地您的$奖金(7000,0,0) 您的$佣金(2000.28, 0,0) >>> –

2

先回到出口(这意味着第二回不工作) 为了克服这种变化的语法 - return (salary, bonus, commision) 编辑 - 这reurns输出作为一个元组,下面的代码将给你如何使用它的想法采取输出

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] 

`

+0

谢谢。回报奖金回报佣金和回报(薪金,奖金,佣金)之间有什么区别?当我这样编辑时,出现这个错误:TypeError:不支持的操作数类型为 - :'tuple'和'在 –

+0

@JuliePixie引用编辑的答案,改变输出访问元组将会处理错误 – Nishad

+0

这就是比我处理更先进一点。我们甚至还没有得到元组。这有点凌驾于我的头上。我假设印刷收入2 [1]这意味着打印工资[1]后,如果月> 60和销售> 100000但返回语法错误 –