* ( tax_percentage / 100.0) annual_salary
Programming Project 3
Programming Projects are to be submitted to gradescope
Due date: Sep 26, Thursday at 7pm
This programming project is an adaptation of Ben Dicken’s Where’s the Money Programming Assignment.
Do not import any modules for this assignment. Do not use built-in functions like max() (you have to write your own). You can use the built-in functions we covered in class: print()
, round()
, input()
, len()
, int()
, float()
, str()
in addition to format()
.
Where’s the Money
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 assignment, 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
.
Overview of the program
This program takes a number of various arguments about how much someone makes, and how much they spend. The program will accept 5 argument values:
- Annual salary
- Monthly mortgage or rent
- Monthly bills
- Weekly grocery/food expenses
- Annual travel
These 5 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 | $ 12,000.00 | 24.0% | ######################## | bills | $ 4,800.00 | 9.6% | ######### | food | $ 10,400.00 | 20.8% | #################### | travel | $ 2,500.00 | 5.0% | ##### | tax | $ 10,000.00 | 20.0% | #################### | extra | $ 10,300.00 | 20.6% | #################### ------------------------------------------------------------------
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).
Calculating the values
The output provided above shows the kind of output this program should produce. Let’s talk a bit about how to compute these values.
The basis for all of the percentages is the annual income argument. All of the percentages are calculated based on this. The next two arguments, rent and bills, are 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 fifth argument is already an annual value, so you don’t need to multiple by number of weeks or months.
You need to write a calculate_tax
function to calculate taxes. The tax percentage is dependent upon the amount of money a person make annually. The tax brackets are as follows:
- [$0, $15k] annually: 10% tax
- ($15k, $75k] annually: 20% tax
- ($75k, $200k] annually: 25% tax
- over $200k annually: 30% tax
You should use one or more if / elif / else statements in the code to determine which tax percentage to use. You can store the tax percentage as an integer variable, with one of the following values: 10, 20, 25, or 30. You can compute the amount of the salary goes to taxes as a particular percentage with this formula:
Here are some tests to verify you are calculating the taxes correctly:
assert calculate_tax(40000) == 8000
assert calculate_tax(10000) == 1000
assert calculate_tax(20000) == 4000
After calculating all of these values programmatically, you should generate the output table.
There is also a tax limit of $75,000
. The tax should be capped at this value, and if the limit is reached, a message indicating this should be printed out after the chart. The message should say:
>>> TAX LIMIT REACHED <<<
Overspending
What happens if the expenditures and taxes are more than the annual salary? This should be represented by negative values in the extra row. You should also print a warning after the chart. 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.
# arguments: salary, mortgage/rent, bills, food, travel
40000, 2000, 300, 150, 4000) wheres_the_money(
------------------------------------------------------------------------------------------------------ See the financial breakdown below, based on a salary of $40000 ------------------------------------------------------------------------------------------------------ | mortgage/rent | $ 24,000.00 | 60.0% | ############################################################ | bills | $ 3,600.00 | 9.0% | ######### | food | $ 7,800.00 | 19.5% | ################### | travel | $ 4,000.00 | 10.0% | ########## | tax | $ 8,000.00 | 20.0% | #################### | extra | $ -7,400.00 | -18.5% | ------------------------------------------------------------------------------------------------------ >>> WARNING: DEFICIT <<<
Formatting a Number
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 the Constants, variables, and comments reading. Here’s an example of how you can use this function to match the formatting in the spec.
Say we have a floating point number, like below:
= 134567.123 number
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.
Add a comma
- Set to stop at two decimal points
- Add necessary spacing before the number, so that the table lines up
Examples for string method .format()
Using the comma as a thousands separator:
'{:,}'.format(1234567890)
'1,234,567,890'
Establishing numbers of digits to hold space for:
'{:15,}'.format(10000)
' 10,000'
Expressing floats with four decimals (the 4
is number of digits after the decimal point, the f
means it’s formatting a float):
'{:.4f}'.format(4.2353635234)
'4.2354'
Expressing a percentage with two decimals:
= 19
points = 22
total 'Correct answers: {:.2%}'.format(points/total)
'Correct answers: 86.36%'