Day 5 In-class Assignment: Savings for Everyone#
✅ Put your name here.#
✅ Put your group member names here.
Learning Goals:#
By the end of this assignment you should be able to:
Review code that uses
if
,elif
, andelse
statments and understand the logical flow of the programWrite
if
/elif
/else
statements in Python.Create and use basic functions in Python.
Part 1: Understanding if
statements in Python#
As you saw in the pre-class assignment, if
statements provide an mechanism for “branching” to occur in your code. Depending on whether or not something is determined to be true, your code can pursue one branch, or another, or do nothing at all. The else
statement allows your code to execute something even if none of the if
or elif
statements you write are found to be true. By combining these sorts of commands in a variety of ways we can create a very complex system of branches in our code.
✅ As a group, review and run the following bit of example code.
✅ As a group, add comments to explain what various parts of the code are doing.
some_numbers = [2, -5, 16, 17, -6, 23, 0]
for i in range(len(some_numbers)):
number = float(some_numbers[i])
if number < 0:
print('Square root is not real.')
else:
print('Square root of',number,'is',number**(1/2))
for number in some_numbers:
if number > 0:
print(number,'is positive.')
elif number < 0:
print(number,'is negative.')
else:
print('The number is zero.')
Square root of 2.0 is 1.4142135623730951
Square root is not real.
Square root of 16.0 is 4.0
Square root of 17.0 is 4.123105625617661
Square root is not real.
Square root of 23.0 is 4.795831523312719
Square root of 0.0 is 0.0
2 is positive.
-5 is negative.
16 is positive.
17 is positive.
-6 is negative.
23 is positive.
The number is zero.
✅ Question: What does float(some_numbers[i])
do?
✎ Put your answer here
✅ Question: What does the len()
function do?
✎ Put your answer here
✅ Question: What does the range()
function do?
✎ Put your answer here
Part 2: Writing your own if
statements#
Sometimes in computational modeling and data science, you need to be able parse a set of numbers or data and execute particular tasks based on the how those numbers compare to each other. Conditional statements that use things like <
, >
, >=
, or ==
combined with logical operators like and
and or
provide an excellent mechanism for comparing values! Try the following:
✅ Write a program that compares three variables v1, v2, and v3 (initial values are provide below) and does the following using if
statements:
If v1 is equal to v2, print “woohoo!”
If v1 is the smallest value of the three (i.e., v1 is less than both v2 and v3), print out “v1 is tiny!”
If v1 is the smallest value of the three, add a nested if statement to print out a statement saying whether v2 or v3 is the largest value.
Try several values of v1, v2 and v3 and ensure that you see the correct behavior.
Suggestion: Make sure you work together with your group to solve this. Planning it out on the whiteboard can also be useful!
Note: Although you learned briefly about functions in your pre-class assignment, don’t worry about using functions just yet!
# Put your code here. Initial values for v1, v2, and v3 are provided.
# Make sure to test out new values to ensure your code works as intended!
v1 = 1
v2 = 4
v3 = 3
✅ Question: What happens in your code if either v2 or v3 is less than v1? If you’re not sure, test it out with a bit of code, you can’t (really) break things by experimenting!
✎ Put your answer here
Part 3: Continued Investment#
You’re back at your bank job, calculating the total amount of money people save in their retirement accounts. Previously, your boss asked you to calculate the different amount of money people would save if there was one interest rate for people that started saving early (under 30) and another interest rate (twice the original interest rate) if they started saving later (30 or older).
Your boss used your calculations to justify to their boss that offering a “better interest rate for older investors” was a good marketing strategy. Your boss’s boss accepted the plan with one modification. Rather than give people over 30 double the interest rates, the interest rates would be structured in the following way:
People that start to save when they are between 18-24 years old will receive an interest rate of 6.5%
People that start to save when they are between 25-31 years old will receive an interest rate of 8.5%
People that start to save when they are between 32-39 years old will receive an interest rate of 11.0%
People that start to save when they are 40 years or older will receive an interest rate of 17.0%
(Also, it is assumed that everyone will retire at 65)
The bank rolled out the plan, and it worked really well. Now, there are dozens of new applications for savings accounts, and your boss says you need to calculate the expected savings for all of these new applications.
You will need to write a piece of code that will go through each of these new applications and calculate each individual’s expected retirement savings. Your code should make a list of all of the expected retirement savings.
Writing complex code can seem intimidating at first, so we are going to break down how to build your code piece by piece and test it along the way! Make sure that everyone in your group is ready to move on to the next step and support each other (or ask an instructor) if you need help!
3.1 Planning out Complex Code#
✅ 3.1.1 Question: When planning out complex code, it can often help to break it down into smaller problems, once you have an idea of the end goal. Start by answering these questions below with your group!
You and your groups members are expected to use the whiteboards in the classroom or a virtual whiteboard as you come up with your plan..#
Things to think about:
How will you go through each application?
What variables will change for each application?
What calculations do you need to make for each application?
How can you use the code you wrote in the previous compound interest assignment?
(Note: Answering these questions is not the same as coming up with a plan!)
Write your answers here
Make sure you run the cell below!! It has the data for each customer. The elements in each list correspodn to each other (e.g. Michael Mitchell is 20 years old and putting $4680 into his account each year).
# New applications
client_name = ["Michael Mitchell","Ashley Moore","Fred Walker","Walter Wood","Jesse Gonzalez","Phillip Sanders","Marie Jenkins","Annie Brown","Christopher King","Carl Phillips","Betty Perez","Ashley Hernandez","Carlos Davis","Richard Jackson","Jean Cox","Sara Allen","Peter Butler","Justin Collins","Kevin Miller","Andrew Russell"]
savings_per_year = [4680,4680,4320,5880,5280,5640,4680,5520,5040,4200,4920,5400,5280,4560,5040,4440,5640,5040,4800,4680]
start_savings_age = [20,31,35,39,40,37,26,21,36,25,25,36,33,27,35,31,28,19,31,34]
✅ 3.1.2 Question: Write down your plan (and/or pseudocode) for code that acesses the data for each customer and uses if
, elif
, and else
statements to identify the cirrect interest rate for each customer.
✎ Record your plan/pseudocode here!
✅ 3.1.3 Task: Now turn that plan into code. Make sure you test your code to ensure it is working the way you expect!
# Write your code here
Now that you have code that sets up the variables you need to calculate the final savings amount for each customer, you need to figure out how to calculate the final savings amount for each customer (and append it to a list).
✅ 3.1.4 Task: Go get your code (from Day 3 or Day 4) to calculate the retirement savings and paste it below. Repurposing old code is a common practice among computational professionals!
# put your code here!
✅ 3.1.5 Task: Now, adapt that code to use the variables you used in 3.1.3 and check it for one customer! Hint: Try printing out the variables from 3.1.3 here. What are their current values? How can that help you test your code?
# put your code here!
✅ 3.1.6 Question: Now, how can we put those two working pieces of code together so that we get the final retirement savings for every customer in the list? In the cell below, write out your plan and/or pseudocode. Hint: You may want to look back at the Day 3 pre-class for how to set up a “nested” loop!
✎ Record your plan/pseudocode here!
🛑 STOP AND ASK YOUR INSTRUCTOR TO COME OVER AND CHECK YOUR PLAN#
3.2 Write out your final code#
✅ Task: Using the lists of all of the new applications from above, and your plan from 3.1.6, create your code that will calculate the final retirement savings for each customer and append that to a list called final_retirement_savings
. The empty list is given to you below to start with!
# Put your code here
final_retirement_savings = []
Once you think you’ve got everything coded up correctly, run the following cell to see how much money everyone ended up with. Make sure to review the print statement formatting techiques used in this code. This could be useful for future assignments.
# Print out the final savings for all of the new applications
for kk in range(len(final_retirement_savings)):
print("{} will save ${:.2f} million by the time they retire".format(client_name[kk],final_retirement_savings[kk]/1.0e6))
Michael Mitchell will save $1.15 million by the time they retire
Ashley Moore will save $0.83 million by the time they retire
Fred Walker will save $0.86 million by the time they retire
Walter Wood will save $0.75 million by the time they retire
Jesse Gonzalez will save $1.54 million by the time they retire
Phillip Sanders will save $0.90 million by the time they retire
Marie Jenkins will save $1.27 million by the time they retire
Annie Brown will save $1.27 million by the time they retire
Christopher King will save $0.90 million by the time they retire
Carl Phillips will save $1.24 million by the time they retire
Betty Perez will save $1.45 million by the time they retire
Ashley Hernandez will save $0.96 million by the time they retire
Carlos Davis will save $1.31 million by the time they retire
Richard Jackson will save $1.14 million by the time they retire
Jean Cox will save $1.00 million by the time they retire
Sara Allen will save $0.78 million by the time they retire
Peter Butler will save $1.29 million by the time they retire
Justin Collins will save $1.33 million by the time they retire
Kevin Miller will save $0.85 million by the time they retire
Andrew Russell will save $1.04 million by the time they retire
✅ Question: Which client will retire with the largest amount of money in their savings account? When did they start saving? How much money did they add every year?
✎ Put your answer here
✅ Question: Which client will retire with the smallest amount of money in their savings account? When did they start saving? How much money did they add every year?
✎ Put your answer here
✅ Question: Compare the people that retired with the most and least amount of money. What was the leading cause of the differences between the two? Is there anything that the person with the least amount of money saved could have done differently?
✎ Put your answer here
🛑 STOP#
Check in with all of your group members to make sure you all understand everything to this point!
Part 4: Writing Functions#
In the pre-class assignment, you were introduced to how to write functions and when to use them.
To review:
Functions are handy when you have code you have to run many times but want to avoid copying and pasting
Functions can prevent bugs by reducing the places you have to change code
Functions can make your code easier to read by allowing you to compartmentalize your code into separate functions which you can then call in the main body of your code
Functions can save future you time by allowing you to write functions you can use in future notebooks!
In general, if a piece of code is repeated more than three times or may be useful later, you should turn it into a function!
In today’s assignment and a previous assignment, you used a block of code that calculated the total amount of money saved for retirement. Let’s use this bit of code to get some practice writing functions.
4.1: Writing a function#
Write a function that calculates the total amount of money saved in a retirement account. Remember, your should define your function to accept all of the appropriate model parameters as input arguments. When you’re finished, you should be able to insert this function directly into the code you wrote in the previous exercise.
# Put your code for your new function here
4.2 Testing your function#
It’s good to get into the habit of testing your function to see if it does what you want/expect it to do.
Try running your function for the investor “Michael Mitchell” from above. Do you get the same answer you got before? When you run your test, can you use the information directly from the lists above to get Michael’s information?
# Test your function with the above inputs here.
Part 4.3: Using your Function#
In the cell below, put your code from Part 3.2. Then, modify the code and use the function you wrote to calculate the total amount of money saved in the retirement account.
# Put your code here
Once you think you’ve got everything coded up correctly, run the following cell to see how much money everyone ended up with. If you get an unexpected error, double-check your code and discuss with your group what might be causing your bug.
# Print out the final savings for all of the new applications
for kk in range(len(final_retirement_savings)):
print("{} will save ${:.2f} million by the time they retire".format(client_name[kk],final_retirement_savings[kk]/1.0e6))
Michael Mitchell will save $1.15 million by the time they retire
Ashley Moore will save $0.54 million by the time they retire
Fred Walker will save $0.86 million by the time they retire
Walter Wood will save $0.37 million by the time they retire
Jesse Gonzalez will save $1.54 million by the time they retire
Phillip Sanders will save $0.90 million by the time they retire
Marie Jenkins will save $1.27 million by the time they retire
Annie Brown will save $1.27 million by the time they retire
Christopher King will save $0.90 million by the time they retire
Carl Phillips will save $1.24 million by the time they retire
Betty Perez will save $1.45 million by the time they retire
Ashley Hernandez will save $0.96 million by the time they retire
Carlos Davis will save $1.31 million by the time they retire
Richard Jackson will save $1.14 million by the time they retire
Jean Cox will save $1.00 million by the time they retire
Sara Allen will save $0.51 million by the time they retire
Peter Butler will save $1.29 million by the time they retire
Justin Collins will save $1.33 million by the time they retire
Kevin Miller will save $0.55 million by the time they retire
Andrew Russell will save $1.04 million by the time they retire
Assignment wrapup#
Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credit for the assignment!
from IPython.display import HTML
HTML(
"""
<iframe
src="https://cmse.msu.edu/cmse201-ic-survey"
width="800px"
height="600px"
frameborder="0"
marginheight="0"
marginwidth="0">
Loading...
</iframe>
"""
)
Congratulations, you’re done!#
Submit this assignment by uploading it to the course Desire2Learn web page. Go to the “In-class assignments” folder, find the appropriate submission link, and upload it there.
See you next class!
© Copyright 2025, Department of Computational Mathematics, Science and Engineering at Michigan State University