Skip to content

Latest commit

 

History

History
90 lines (52 loc) · 4.35 KB

README.md

File metadata and controls

90 lines (52 loc) · 4.35 KB

Budget Buddy

Run on Repl.it

The Goal

Today we're going to write a program that can help make sure you don't ever accidentally spend too much money at the grocery store by writing a budget app.

A budget is a breakdown of where you plan to spend your money before you actually spend it - it can be easy to spend money on fun things the day you get your paycheck, because it feels like you have all this extra money!

Homer spends too much on donuts

However, it's important to remember to keep money aside for important purchases like groceries, or to save it up for something that matters to you.

If you want more context on what budgeting is and why people do it before starting the lab, you can learn more here.

Our Program

We're going to create several different budget checkers. Each one will let a user enter their total before they swipe their card or hand over cash at the register, just to make sure they're not about to overspend.

Some users will want it super simple, so we will write a simple budget checker for them. It will only give them a message if they're about to spend too much money.

Other users will want to know how much money they have left to spend, or how far over budget they are. That's a more complex tool, so we will build it after we've got the simple one done.

We'll start with a budget of 100 dollars, meaning we can spend that much according to our plan. Anything over 100 dollars is considered overspending.

Defining and Testing

This lab is broken into two parts. The definitions of all our budgets will be in a file called budget.py. We're only DEFINING budgets checkers here, not testing them yet.

We will TEST our budget checkers using the budgetTest.py file. We can run those tests with the command python budgetTest.py in our console / terminal / command line.

If you feel ready to jump into the lab, then go for it! If you'd like a little refresher or if you get stuck, read on through the main concepts the lab will cover.

Useful Skills & Vocab

Defining versus calling functions

Implementing functions in a program is a two-step process. Functions must first be defined and then they can be called.

Defining
def my_test_function():
  return "Hello! Your test has run successfully!"
Calling
print(my_test_function())

If a function is never called, then your program COULD use it, but it never knows when to do so.

Returning versus printing output

We're relying on returning output when we define our functions, but we're using print statements in our tests so that those return values are visible. Remember, printed data can be seen, returned data can stored in variables or otherwise used.

For a refresher course on the difference, jump back to the String Music Theory lab and read over the notes on printing vs. returning.

Using multiple arguments

One of the more advanced budget checkers will take two arguments. For example, if you wanted to know how much it costs to buy a family tickets to Disneyland, you'd need TWO pieces of information.

Take a look at this screenshot from Disney's website:

Disney Tickets Screenshot

You need to know how many adults (considered 10 years old or older) and how many children (ages 3-9) will be attending. SO let's write out the definition of that function.

def cost_of_disney(adults, children):
  adult_price = adults * 199
  child_price = children * 187
  total_cost = adult_price + child_price
  return total_cost

Now that we can calculate the cost of tickets, let's do that for this family:

Disney Family

Let's assume the two smaller children are both between the ages of 3 and 9, so then this family has 6 adults and 2 children.

Find the total cost by calling (and printing!) the function.

print(cost_of_disney(6, 2))