Budgeting and money management is very important both at the individual level, and for corporations and government. Having a clear understanding of where your money goes, how much you are saving, and how much you invest is important to financial success. As a famous author once wrote:
Annual income twenty pounds, annual expenditure nineteen six, result happiness.
Annual income twenty pounds, annual expenditure twenty pound ought and six, result misery.
–Charles Dickens
In this PA, you will be writing a program that helps a user visualize and understand how much money they spend of various categories of expenditures.
Perhaps you will even find this program useful for your personal finances!
You should name your program wheres_the_money.py
.
This program works by accepting a number of various inputs from a user about how much they make, and how much they spend. The program will accept 6 input values. After printing out a welcome message, the program will request all 6 of these inputs. Below is an example of these input requests, with values provided:
What is your annual salary?
50000
How much is your monthly mortgage/rent?
1200
What do you spend on bills monthly?
300
What are your weekly grocery/food expenses?
200
How much do you spend on travel annually?
5000
Tax percentage?
15
These 6 values will be used to generate a visualization of the financial situation that looks like so:
---------------------------------------------------------------------
See the financial breakdown below, based on a salary of $50000
---------------------------------------------------------------------
| mortgage/rent | $ 14,400 | 28.8% | ############################
| bills | $ 3,600 | 7.2% | #######
| food | $ 10,400 | 20.8% | ####################
| travel | $ 5,000 | 10.0% | ##########
| tax | $ 7,500.0 | 15.0% | ###############
| extra | $ 9,100.0 | 18.2% | ##################
---------------------------------------------------------------------
As you can see, the result is essentially a 6-row, 4-column grid that neatly shows that amounts and percentages of income go where.
Each row represents an expense (or other category) where money goes to on a yearly basis.
The first columns provides the name of the category.
The second column shows the amount of money that goes to this category in dollars.
The third columns shows the amount of money as a percentage of yearly income.
The last column is a horizontal bard that correlates with the percentage of income.
The number of #
characters in this column should be the same as the percentage of income (rounded down).
You’ve now seen the input and outputs that this program accepts and produces. Let’s talk a bit about how to compute these values.
The basis for all of the percentages is the first input value - the annual income. All of the percentages are calculated based on this. The next two inputs, rent and bills, monthly expense values. In order to get the total spend on these categories annually, these numbers must be multiplied by the number of months in a year. Then, you can use this result to calculate the percentage. The fourth value is a weekly expense, so this must instead be multiplied by the number of weeks in a year. The firth value is already input as an annual value, so you don’t need to multiple by number of weeks or months.
The last value is an integer with represents the tax percentage. In the example shown earlier, the value entered was 15, meaning that taxes are %15 of income. You can compute the amount of the salary goes to taxes as a particular percentage with this formula:
annual_salary * ( tax_percentage / 100.0)
After calculating all of these values programmatically, you should generate the output table.
What happens if the expenditures and taxes are more than the annual salary? This should be represented by negative values in the extra row. Below is an example of a financial situation in which the user spends more than they earn, resulting in a deficit in the extra row.
-----------------------------
----- WHERE'S THE MONEY -----
-----------------------------
What is your annual salary?
50000
How much is your monthly mortgage/rent?
2200
What do you spend on bills monthly?
300
What are your weekly grocery/food expenses?
100
How much do you spend on travel annually?
4000
Tax percentage?
32
---------------------------------------------------------------------------------------------
See the financial breakdown below, based on a salary of $50000
---------------------------------------------------------------------------------------------
| mortgage/rent | $ 26,400 | 52.8% | ####################################################
| bills | $ 3,600 | 7.2% | #######
| food | $ 5,200 | 10.4% | ##########
| travel | $ 4,000 | 8.0% | ########
| tax | $ 16,000.0 | 32.0% | ################################
| extra | $ -5,200.0 | -10.4% |
---------------------------------------------------------------------------------------------
You are required to validate the inputs that your program accepts. You should use if-statements for the validation. You must perform the following validation:
Must enter positive integer number.
Tax must be between 0% and 100%.
Multiple test cases will be provided that show what the program should print when these issues occur. Check The differ tool and/or gradescope.
Notice that the numbers are formatted in a particular way in the output table.
You must match this formatting exactly.
In order to do so, you can use the format()
function.
This function was mentioned in chapter two of the textbook.
I will provide one example of how you can use this function to match the formatting in the spec.
Say we have a floating point number, like below:
number = 134567.123
Perhaps this is a result you get for one of the dollar values after doing the necessary computations. When printing, you’ll want to adjust the way it is printed in several ways.
You could print out this number and add these three things:
string_to_print_out = format(number, '15,.1f')
The 15
indicates that the resulting string should be 15 characters wide.
Any characters not needed to display the number will be added as padding to the left side.
The ,
indicates that commas should be added to the number.
The 1
indicates that one decimal place should be shown.
Use this function (with different formatting options) to print out the formatted percentages and dollar values that you calculate.
You should also include a comment at the top of the code file. The comment should include your name and a short description of what the program does. Below is a template you may use:
###
### Author: Your Name Here
### Description: Describe your program with one
### or more sentences of text.
###
You should also make sure to follow other good style principles, such as naming variables well writing having well-formatted and easy-to-read code. Part of the grade for the PA will be for code design and style.
Below are a few complete examples of running wheres_the_money.py
with all 6 input values provided.
These examples will be a part of the test suite on gradescope as well.
-----------------------------
----- WHERE'S THE MONEY -----
-----------------------------
What is your annual salary?
50000
How much is your monthly mortgage/rent?
1200
What do you spend on bills monthly?
300
What are your weekly grocery/food expenses?
200
How much do you spend on travel annually?
5000
Tax percentage?
15
---------------------------------------------------------------------
See the financial breakdown below, based on a salary of $50000
---------------------------------------------------------------------
| mortgage/rent | $ 14,400 | 28.8% | ############################
| bills | $ 3,600 | 7.2% | #######
| food | $ 10,400 | 20.8% | ####################
| travel | $ 5,000 | 10.0% | ##########
| tax | $ 7,500.0 | 15.0% | ###############
| extra | $ 9,100.0 | 18.2% | ##################
---------------------------------------------------------------------
-----------------------------
----- WHERE'S THE MONEY -----
-----------------------------
What is your annual salary?
40000
How much is your monthly mortgage/rent?
750
What do you spend on bills monthly?
400
What are your weekly grocery/food expenses?
100
How much do you spend on travel annually?
3500
Tax percentage?
25
------------------------------------------------------------------
See the financial breakdown below, based on a salary of $40000
------------------------------------------------------------------
| mortgage/rent | $ 9,000 | 22.5% | ######################
| bills | $ 4,800 | 12.0% | ############
| food | $ 5,200 | 13.0% | #############
| travel | $ 3,500 | 8.8% | ########
| tax | $ 10,000.0 | 25.0% | #########################
| extra | $ 7,500.0 | 18.8% | ##################
------------------------------------------------------------------
This PA is due on Friday, September 7th at 5pm. Turn in the program via Gradescope. You should make sure that all of the test cases pass before you turn it in. You can still submit without all of the cases passing, but that is not preferable.