Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Balance and Interest

An online bank wants you to create a program that shows prospective customers how their deposits will grow. Your program should read the initial balance and the annual interest rate. Interest is compounded monthly. Print out the balances after the first three months. How should this Python code look?

Python Beginner

1 Answer

This is one way of doing it:

percent_rate = int(input("Enter the percnet interest rate: ").strip())
decimal_rate = percent_rate / 100
initial_balance = int(input("Enter the initial balance: ").strip())

def calculate_balance(month):
    return initial_balance * ((1 + decimal_rate / 12) ** month)

for month in range(1, 4):
    print("The balance at the end of month {} is: {}".format(month, calculate_balance(month)))

I tested the numbers quickly with 100% interest rate and an initial balance of $1.00, since they make the calculations easy, and I think I got the right numbers.