# Accept the inputs startBalance = float(input("Enter the investment amount: ")) years = int(input("Enter the number of years: ")) rate = int(input("Enter the rate as a %: ")) # Convert the rate to a decimal number rate = rate / 100 # Initialize the accumulator for the interest totalInterest = 0.0 #Display the header for the table print("%4s%18s%10s%16s" % \ ("Year", "Starting balance", "Interest", "Ending balance")) # Compute and display the results for each year for year in range(1, years + 1): interest = startBalance * rate endBalance = startBalance + interest print("%4d%18.2f%10.2f%16.2f" % \ (year, startBalance, interest, endBalance)) startBalance = endBalance totalInterest += interest # Display the totals for the period print("Ending balance: $%0.2f" % endBalance) print("Total interest earned: $%0.2f" % totalInterest)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question