Assignment 2

Goals

The goal of this assignment is to work on control structures and functions in Python.

Instructions

You will be doing your work in a Jupyter notebook for this assignment. You may choose to work on this assignment on a hosted environment (e.g. tiger) or on your own local installation of Jupyter and Python. You should use Python 3.12 for your work. To use tiger, use the credentials you received. If you work remotely, make sure to download the .ipynb file to turn in. If you choose to work locally, Anaconda, miniforge, or uv are recommended methods to install and manage Python. If you work locally, you may launch Jupyter Lab either from the Navigator application or via the command-line as jupyter lab.

Due Date

The assignment is due at 11:59pm on Monday, February 3.

Submission

You should submit the completed notebook file required for this assignment on Blackboard. The filename of the notebook should be a2.ipynb.

Details

Please make sure to follow instructions to receive full credit. Use a markdown cell to Label each part of the assignment with the number of the section you are completing. You may put the code for each part into one or more cells.

In this assignment, we will be working with compound interest. When you have money in a savings account, you usually earn interest on those funds. However, that interest may be computed monthly, yearly, or at other intervals. Suppose you were given a choice of one of two savings accounts as a gift with the rule that you could only access the money in 20 years. The first account currently has $1,000 but earns 5% interest while the second account currently has $2,000 but earns 2% interest. Which account would you choose?

0. Name & Z-ID (5 pts)

The first cell of your notebook should be a markdown cell with a line for your name and a line for your Z-ID. If you wish to add other information (the assignment name, a description of the assignment), you may do so after these two lines. Because we are concentrating on control flow and functions, do not use lists, other collections, or comprehensions for this assignment.

1. The Interest Function (10 pts)

Write a function total_with_interest that takes two numeric values, the starting amount of money and the yearly interest rate, and computes the amount of money after one year. The formula is \[ A = P(1 + r) \] where \(P\) is the starting amount, \(r\) is the rate, and \(A\) is the total with interest. Test your function with different values.

2. Time to Wait (10 pts)

Now, write a function years_until_target that takes the arguments from Part 1 plus an additional argument, target, that specifies the amount of money you would like to save. Use a loop construct to compute how many years it will take to grow the funds to that amount given the starting amount and interest rate. For example, if you start with $1,000 and put it into an account earning 1% (r=0.01) interest, your function should compute that it will take 70 years to have at least $2,000.

Hints
  • Consider a while loop
  • Make sure to use the correct type of boolean comparison

3. Comparing Scenarios (15 pts)

Now, given two different scenarios (starting amount \(A_1\), interest rate \(r_1\)) and (starting amount \(A_2\), interest rate (\(r_2\)), calculate (1) whether the smaller of \(A_1\) or \(A_2\) will eventually surpass the other and if so, (2) how many years that will take. If the smaller of \(A_1\) or \(A_2\) will never catch up, return float('inf'), otherwise return the number of years for the initially smaller value to surpass the larger. Your function years_to_catch should take four arguments, \(A_1\), \(r_1\), \(A_2\), and \(r_2\). This helps answer the question described at the beginning of the assignment. For example, if \(A_1 = 1000\), \(r_1=0.05\), \(A_2 = 2000\), and \(r_2 = 0.02\), the function should return 24. However, if \(r_1 = 0.02\) instead, the function should return inf.

Hints
  • Make sure you don’t get stuck in an infinite loop. If you do, you can interrupt the computation using the stop button in Jupyter.
  • Use simultaneous assignment to swap the scenario variables

4. Effect of the Number of Compounding Periods (15 pts)

Many banks compound interest monthly, meaning they calculate the interest at the end of each month (twelve times a year) instead of at the end of each year (once a year). Calculating this impact requires tweaking the interest function to add in the number of periods. With \(n\) periods, our equation changes to: \[ A = P(1 + r/n)^n \] Note that this still only computes one year’s interest so we need to iteratively multiply to get additional years. Given an initial amount \(P\), write a table that shows the effect of compounding interest for differing values of \(n\) from 1 to 12. Each line should show the total amount in the account at the start of each year for eleven years. Round to the nearest cent. For example, if \(P=1000\) and \(r=0.05\), we see

1 : 1000 1050.0 1102.5 1157.62 1215.51 1276.28 1340.1 1407.1 1477.46 1551.33 1628.89 
2 : 1000 1050.62 1103.81 1159.69 1218.4 1280.08 1344.89 1412.97 1484.51 1559.66 1638.62 
3 : 1000 1050.84 1104.26 1160.4 1219.39 1281.38 1346.53 1414.98 1486.91 1562.51 1641.94 
4 : 1000 1050.95 1104.49 1160.75 1219.89 1282.04 1347.35 1415.99 1488.13 1563.94 1643.62 
5 : 1000 1051.01 1104.62 1160.97 1220.19 1282.43 1347.85 1416.6 1488.86 1564.81 1644.63 
6 : 1000 1051.05 1104.71 1161.11 1220.39 1282.7 1348.18 1417.01 1489.35 1565.39 1645.31 
7 : 1000 1051.08 1104.78 1161.21 1220.53 1282.88 1348.42 1417.3 1489.71 1565.81 1645.79 
8 : 1000 1051.11 1104.83 1161.29 1220.64 1283.03 1348.6 1417.52 1489.97 1566.12 1646.16 
9 : 1000 1051.13 1104.87 1161.35 1220.73 1283.14 1348.74 1417.69 1490.17 1566.36 1646.44 
10 : 1000 1051.14 1104.9 1161.4 1220.79 1283.23 1348.85 1417.83 1490.34 1566.55 1646.67 
11 : 1000 1051.15 1104.92 1161.44 1220.85 1283.3 1348.94 1417.94 1490.47 1566.71 1646.85 
12 : 1000 1051.16 1104.94 1161.47 1220.9 1283.36 1349.02 1418.04 1490.59 1566.85 1647.01 
Hints
  • Remember how the parameters for range work (what the first and last numbers included are). Consult the documentation if necessary.
  • The print method has an optional keyword argument end that may be helpful in keeping the output on one line.
  • You can use the round method to limit the number of decimal places displayed to 2.