0

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?

1
  • 2
    You didn't assign the return value to anything. Commented Nov 11, 2016 at 5:00

2 Answers 2

2

You are not assigning any value from AddInterest:

amount = addInterest(amount, rate)
Sign up to request clarification or add additional context in comments.

Comments

1

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.