Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Update the Amount

reapyt on May 3, 2024

I don’t understand the way amount is updated in the invest function provided in this video. Why do we add 1 to rate?

def invest(amount, rate, years):
    for year in range(1, years + 1):
        amount = amount * (1 + rate)
        print(f"{year}: ${amount:.2f}")

I came up with this solution:

def invest(amount, rate, years):
    for year in range(1, years + 1):
        amount += amount * rate
        print(f"{year}: ${amount:.2f}")

And I also understand the solution given for the same exercise in the Python Basics book:

def invest(amount, rate, years):
    for year in range(1, years + 1):
        gain = amount * rate / 100
        amount = amount + gain
        print(f"year {year}: €{amount:.2f}")

Any help would be appreciated. Thank you in advance.

Become a Member to join the conversation.