我已经定义了这两个函数,我需要从第一个函数调用函数2中的收入和容差,基本上我想计算函数2中的finalIncome(该行代码被注释)。下面是代码:
def personalAllowance():
income = int(input("Enter your annual salary: £"))
allowance = 10600
if(income>100000):
for i in range (100000, income):
if(income%2==0):
allowence = allowence - 0.5
if(allowence<0):
allowence = 0
print("Personal Allowance = " + str(allowence))
else:
print("Personal Allowance = " + str(allowence))
return (income , allowance)
def incomeTax():
print("\n")
#finalIncome = income - allowence
print(finalIncome)
taxBill = 0
if(finalIncome <= 31785):
taxBill = finalIncome * (20/100)
elif(finalIncome > 31785 and finalIncome < 150000):
taxBill = finalIncome * (40/100)
elif(finalIncome >= 150000):
taxBill = finalIncome * (45/100)
print (taxBill)
incomeTax()
答案 0 :(得分:2)
你只需要调用personalAllowance
并将返回值分配给某个东西。
例如:
income, allowance = personalAllowance()
答案 1 :(得分:2)
保存对这些值的引用,然后减去它们:
income, allowance = personalAllowance()
finalIncome = income - allowance
答案 2 :(得分:1)
因为你实际上并不需要&#34;收入&#34;或者&#34; allowance&#34;而不是返回一个元组,只返回差异,如图所示
def personalAllowance():
income = int(input("Enter your annual salary: £"))
allowance = 10600
if(income>100000):
for i in range (100000, income):
if(income%2==0):
allowence = allowence - 0.5
if(allowence<0):
allowence = 0
print("Personal Allowance = " + str(allowence))
else:
print("Personal Allowance = " + str(allowence))
return income - allowance ## Just return the difference
def incomeTax():
print("\n")
finalIncome = personalAllowance() ## This will return difference
print(finalIncome)
taxBill = 0
if(finalIncome <= 31785):
taxBill = finalIncome * (20/100)
elif(finalIncome > 31785 and finalIncome < 150000):
taxBill = finalIncome * (40/100)
elif(finalIncome >= 150000):
taxBill = finalIncome * (45/100)
print (taxBill)
incomeTax()