[英]Calling variable from another function in python
I have defined these two functions and I need to call income and allowance in function 2 from the first function, basically I want to calculate the finalIncome in function 2 (that line of code is commented). 我已经定义了这两个函数,我需要从第一个函数中调用函数2中的收入和津贴,基本上我想计算函数2中的finalIncome(注释该行代码)。 Heres the code: 这是代码:
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()
You just have to call personalAllowance
and assign the return value to something. 您只需要调用personalAllowance
并将返回值分配给某些对象即可。
For example: 例如:
income, allowance = personalAllowance()
Save references to those values and then subtract them: 保存对这些值的引用,然后减去它们:
income, allowance = personalAllowance()
finalIncome = income - allowance
Since you don't actually need the "income" or "allowance", instead of returning a tuple, just return the difference as shown where I have commetned 由于您实际上并不需要“收入”或“津贴”,因此无需返回元组,只需返回与我相比较的地方所示的差额即可
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.