Savings Calculator Project
About the project
This lesson is a project. It lets you use what you've learned to build a larger program that has real-world relevance.
To complete the project you'll need to use what you've learned about printing, variables, numbers, and strings.
The point of the project is to build a program that can calculate yearly savings.
Project description
Bob wonders how much money he can save in a year. Help him figure it out by writing a Python program for him.
The program should calculate how much a person can save in a year based on their hourly income, hours worked, and weekly cost of living.
We'll build up your program gradually, one step at a time.
Greeting Bob
Before doing any calculations we'll print out a greeting for Bob. We want our program to output:
Hello Bob
We'll put the greeting and the name in variables so we can reuse them later in the project.
Define a variable named greeting
that holds the value "Hello "
. (Remember
the space at the end.)
Then define a variable called name
that holds the value "Bob"
.
Then print greeting + name
.
Calculating weekly income
Now that we've greeted Bob properly, we can start calculating savings based on Bob's income.
Bob earns $20/hour and he works 40 hours per week. Store these numbers in the
variables hourly_wage
and hours_per_week
.
(Don't put the dollar sign anywhere in the code; we'll implicitly be working with dollars for this project.)
Then multiply the two variables to find Bob's weekly income. Store it in a
variable named income_per_week
.
From weekly to yearly income
Bob works 48 weeks every year. Define a variable named weeks_per_year
and
set it to 48
.
Then multiply the weeks_per_year
with the income_per_week
to
calculate the yearly income. Store it in a variable named income_per_year
.
What did Bob earn?
Let's figure out how much income Bob has in a year.
First print the string name + "'s yearly income is:"
.
Then do another print, this time with the income_per_year
variable.
Paying rent
We now know that Bob currently earns about $38k per year. But Bob must pay for food and rent. This costs him $400/week for all 52 weeks of the year.
So define a variable named spend_per_week
which is 400
and then calculate
spend_per_year
as spend_per_week * 52
.
Then print name + "'s yearly spend is:"
.
Then print the spend_per_year
variable.
Combining previous code
Now we've calculated the income_per_year
and the spend_per_year
. We're
almost ready to subtract the two to figure out how much Bob can save in a year.
But first let's combine your previous solutions into one large program.
It might be hard to keep an overview of the code at this point because there's a lot of it. It helps to add some blank lines between code sections.
You can also add lines starting with #
which are comments. These are only
for you to benefit from; Python ignores them.
In the code box, we've added some comments and spacing and combined your old solutions.
Since we've just copied each of your solutions, the name
variable is defined
three times. Remove the last two definitions. Then run the code to check that it still works.
Calculating savings
Bob will save all the money he doesn't have to spend.
At the bottom of your program, you will find this comment:
# --- Calculate the yearly savings ---
Below that line, calculate Bob's yearly savings and store the savings in a
variable named savings_per_year
. Bob's savings is the difference between his
yearly income and his yearly spend, so subtract the two.
Then print name + "'s yearly savings:"
.
Then print the savings_per_year
variable.
Then your program is finished!
Your solution so far
By now you've solved Bob's problem! He can save about $17k per year with his current lifestyle.
We've stored all the information in variables so far. That's smart because it means that if any of the information changes, modifying the program is straightforward.
Going part-time
Bob suddenly changes his mind. Rather than saving money, he decides that he'd like to work part-time for 25 hours per week. He figures that by living frugally, he can cut his costs down to $300/week. Is this sustainable, or will he not have enough money for his costs?
Change the variables hours_per_week
and spend_per_week
and see if
Bob's yearly savings go negative, making his proposed lifestyle unsustainable.
A program for Alice
You've now calculated that working part-time, Bob can still save up about $8k yearly, so Bob can indeed live this way.
Working part-time, Bob now has more time for dating. He meets Alice and wants to show her the program.
Alice earns $30/hour, works 45 hours per week for 46 weeks per year, and spends $500 each week. Change the variables and figure out Alice's yearly earnings.
Conclusion
You've now built a decently-sized program that uses variables, strings, numbers, math, and printing. This shows a deep understanding of all these concepts. Congratulations!
You've also seen why variables are useful in real programs; they give descriptive names for values, and they make understanding and changing your program easy.