调用两个后仅运行一个方法

时间:2018-11-06 08:24:53

标签: python

我想在我的薪水方法之后调用tax_calculator方法来计算用户的税后工资。我得到的只是“你每年赚多少钱?”循环播放。

def salary():
    print("How much do you earn annually? ")
    salary = float(raw_input())


def basic_rate(salary):
    salary_after_tax = 'Your salary after tax is ' + str(salary * 0.8)
    return salary_after_tax

def higher_rate(salary):
    salary_after_tax = 'Your salary after tax is ' + str(salary * 0.6)
    return salary_after_tax

def additional_rate(salary):
    salary_after_tax = 'Your salary after tax is ' + str(salary * 0.55)
    return salary_after_tax

def tax_calculator():
    if salary > 11850 and salary <= 46350:
        basic_rate(salary())
    elif salary > 46350 and salary <= 150000:
        higher_rate(salary())
    elif salary > 150000:
        additional_rate(salary())
    else:
        return "Your salary isn't taxed"

salary()
tax_calculator()

2 个答案:

答案 0 :(得分:0)

您的代码几乎没有问题。解决方法如下(查找评论)

def get_salary(): # AVOID naming variables and functions the same
    print("How much do you earn annually? ")
    salary = float(raw_input())
    return salary # return salary here    

def basic_rate(salary):
    salary_after_tax = 'Your salary after tax is ' + str(salary * 0.8)
    return salary_after_tax

def higher_rate(salary):
    salary_after_tax = 'Your salary after tax is ' + str(salary * 0.6)
    return salary_after_tax
    get_salary()
def additional_rate(salary):
    salary_after_tax = 'Your salary after tax is ' + str(salary * 0.55)
    return salary_after_tax

def tax_calculator():
    salary = get_salary() # capture the return value of get_salary here

    if salary > 11850 and salary <= 46350:
        return basic_rate(salary) # Return the function's return again
    elif salary > 46350 and salary <= 150000:
        return higher_rate(salary) # Return the function's return again
    elif salary > 150000:
        return additional_rate(salary) # Return the function's return again
    else:
        return "Your salary isn't taxed"

# get_salary() DO NOT CALL THIS

print(tax_calculator()) # Print to see results

输出:

How much do you earn annually? 
30000
Your salary after tax is 24000.0

答案 1 :(得分:0)

尝试一下:

def salary():
     print("How much do you earn annually? ")
     salary_new = int(input())
     tax_calculator(salary_new)


 def basic_rate(salary_new):
     salary_after_tax = 'Your salary after tax is ' + str(salary_new * 0.8)
     return salary_after_tax

 def higher_rate(salary_new):
     salary_after_tax = 'Your salary after tax is ' + str(salary_new * 0.6)
     return salary_after_tax

 def additional_rate(salary):
     salary_after_tax = 'Your salary after tax is ' + str(salary_new * 0.55)
     return salary_after_tax

 def tax_calculator(salary_new):
     rate = ''              
     if salary_new > 11850 and salary_new <= 46350:
         rate = basic_rate(salary_new)             
     elif salary_new > 46350 and salary_new <= 150000:
         rate = higher_rate(salary_new)               
     elif salary_new > 150000:         
         rate = additional_rate(salary_new)
     else:                                 
         rate = "Your salary isn't taxed"
     print(rate)  

 salary()