Short Project 5

Calculating Investments

In this short project, you will calculate how many years it takes for an account balance to reach an investment goal.

Name the program investment.py. Make sure that gradescope gives you the points for passing the test cases.

Description of the problem

Here’s the problem to solve:

Imagine you have X amount of dollars in an investment account that earns 5% interest per year. How long (in years) does it take for the account balance to be times Y the original investment?

# X amount of dollars is $10,000
initial_amount = 10000

# times Y is 2
goal_of_investment = 2

message = investment_years(initial_amount, goal_of_investment)
print(message)
The investment of $10,000 will be 2 times larger in 15 years.

Development strategy

You will need to use a while loop to solve this problem. You need to calculate what the goal amount is, and you will calculate how much each passing year accrues until you get to the goal.

You will also need to create a year variable to count how many years it takes to achieve the goal. Remember to increment year with every iteration.

Calculate the interest (multiply balance by 0.05) and the balance (add interest to it) in each iteration as well.

Test cases

def main():
    # X amount of dollars is $10,000
  initial_amount = 10000

  # times Y is 2
  goal_of_investment = 2

  message = investment_years(initial_amount, goal_of_investment)
  print(message) 
  # The investment of $10,000 will be 2 times larger in 15 years.

  print(investment_years(50000, 10))
  # The investment of $50,000 will be 10 times larger in 48 years.
  
main()
Before You Begin

This section gives you a quick recap of what we covered in class or introduces any new tips or examples that might help you complete the assignment. Take a few minutes to read through it before you begin.

How to write a while loop

You need to determine two things:

  1. What’s your condition to stop the iteration
  2. What variables do you need to update the condition in each iteration (so you don’t fall into an infinite loop)

Example

Write a while loop to count how many years until you can run for president (simplistic example, for illustration purposes only):

def count_years(current_age, age_goal):
  years = 0
  age = current_age
  while age < age_goal:
    years += 1
    age += 1
  return years

def main():
  print( count_years(20, 35))
  
main()
15

.format() method

Let’s update our function so it returns a string message instead of an integer:

def count_years(current_age, age_goal):
  years = 0
  age = current_age
  while age < age_goal:
    years += 1
    age += 1
  message = "It will take {} years".format(years)
  message += " for you to be able to run for president."
  return message

def main():
  print( count_years(20, 35))
  
main()
It will take 15 years for you to be able to run for president.

Formatting numbers

Use : to specify formatting options,

amount = 10000000
print("I have ${:,} in my pocket.".format(amount))
I have $10,000,000 in my pocket.