Reputation: 433
I wrote the following code:
def addInterest(balance, rate):
newBalance = balance * (1+rate)
return newBalance
def test():
amount=1000
rate=0.05
addInterest(amount, rate)
print("" ,amount)
test()
I expected the output to be 1050, but it is still printing 1000. Can someone tell me what am I doing wrong?
Upvotes: 0
Views: 74
Reputation: 509
The function addInterest() returns the value 1050, but not apply the changes at amount variable, cos you didn't pass as a referenced variable (i think python doenst support referenced variables). You must to store the returned value into a new variable:
def addInterest(balance, rate):
newBalance = balance * (1 + rate)
return newBalance
def test():
amount = 1000
rate = 0.05
# STORE RETURNED VALUE
result = addInterest(amount, rate)
# PRINT RETURNED VALUE
print(result)
test()
Upvotes: 1
Reputation: 238209
You are not assigning any value from AddInterest
:
amount = addInterest(amount, rate)
Upvotes: 2