simple.py: 6 points
compounding.py: 6 points
In finance, there are two popular ways of calculating interest earned on savings and/or loans. The difference is in the formula used and how often interest is “earned.” In this part of the lab, we will explore both.
The formula for simple interest earned is
$$ interest = principal \times interestRate \times time $$
Your goal is to write a program simple.py
that asks the user for three numbers. The numbers are
p
: an amount of money to invest in savings, called the principalr
: an interest rate you will earn on your savings (paid by the bank for lending them the money - entered as a decimal)t
: an amount of time (in years) that you will keep the money in savingsYour program should then output to the user the amount of money they would earn in simple interest. For example, one run of the program might be:
Welcome to my amazing simple interest prediction program!
How much would you like to save? 1000
What is your interest rate? 0.05
How long are you saving the money? 2
Your interest earned would be $100.0!
ReadMe
You will want to convert the user inputs into decimal numbers because by default, what the user types is considered a string literal. You can do this by using the built-in Python float()
function:
p = float(input("How much would you like to save? "))
This code first uses the input()
command to ask the user for a number (which gives back their answer as a string), then converts the user’s input into a decimal number.
Python also has the opposite built-in function, called str()
, which converts a float
into a string literal. This function actually works for almost any data type (e.g., int
s) in Python. If you wanted to print out the sum of two numbers, you might do the following:
x = 2 + 2
print('The sum of 2 + 2 is ' + str(x))
This is important to remember because string concatenation does not work with non-string types.
You should test your program to make sure it’s calculating the correct interest amounts. Try a variety of numbers for each input to see if the output matches what you calculate by hand. Do not worry about rounding your final output to the nearest cent – it can have as many decimal places as it calculates.
A second option for calculating interest is to use continuously compounding interest. The formula for this type of interest earned is
$$ interest = principal \times 2.71828^{(interestRate \times time)} - principal $$
This formula awards interest constantly (more often than every second!), rather than at the end of the savings. To raise a number to an exponent in Python, we use the exponentiation operator **
. For example, $x^2$ would be written in Python as x**2
.
Your goal is to copy your simple.py
program to a new file called compounding.py
and change the formula used to calculate the continuously compounding interest earned. For example, one run of your program might be:
Welcome to my *even more amazing* continuously compounding interest prediction program!
How much would you like to save? 1000
What is your interest rate? 0.05
How long are you saving the money? 2
Your interest earned would be $105.17084373602802!
Don’t forget to test your program on a variety of values of inputs.
Once you’ve finished your program, don’t forget to commit and push your code before you move onto the next part of the lab. Instructions on how to do so can be found in the Warmup