Course Overview | Previous Session | Next Session
Buying Puppy Food
Every day, my mom pays me two dollars for doing chores around the house. I use this money to buy dog food for Sam. Each portion costs $1.25, and Sam eats two portions a day, which means I need $2.50 per day. I’ve been saving all my money for the past week. Will I have enough to buy food for Sam? Let’s write a software program to find out how much money I need to care for Sam.
- Cost per portion of puppy food: $1.25
- Portions needed per month: 60
Let’s Write a Python Program
We’ll write a Python program to calculate the total monthly cost of puppy food.
Hint! Always press ENTER when you finish a line.
>>> one_portion = 1.25
>>> portions_needed = 60
>>> total_cost = one_portion * portions_needed
>>> print(total_cost)
If you wrote the code correctly, the output should be: $75
Explanation
- Variable Creation:
First, we create two variables,one_portion
andportions_needed
, and assign them the respective values. - Multiplication Calculation:
We use the multiplication operator (*
) to calculate the total cost by multiplying the cost of one portion by the number of portions needed.
To type the multiplication symbol (operator), pressShift + 8
- Displaying the Result:
Finally, we print the total cost usingprint(total_cost)
Can I afford to keep the puppy?
Since my allowance is only $60 per month and dog food costs $75, I want to do more chores, but mom doesn’t have any additional ones available. Instead, she suggested that buying puppy food in bulk (a big box) might be cheaper. I saw a big poster at the store advertising a box containing 60 portions for $59.99, tax included.
- Big box cost: $59.99
- Portions in one big box: 60
Based on this bulk price, I can afford to feed Sam every month. But how much will it cost to feed Sam for an entire year? Let’s write a Python program to find out.
>>> box_cost = 59.99
>>> months = 12
>>> cost_per_year = box_cost * months
>>> print(cost_per_year)
By entering this code, you should get the total cost of dog food per year, which is $719.88. It’s quite a bit, but I can afford it because mom pays me for doing little jobs around the house.
One More Challenge
Every year, my aunt gives me $50 for my birthday. I plan to use that money to buy a leash so I can start taking Sam for walks every day. When I told Auntie that the leash I like costs $32, she asked me how much money I would have left over after buying it. Good question. Let’s write a Python program to find out.
>>> birthday_present = 50.00
>>> leash_cost = 32.00
>>> left_over_money = birthday_present – leash_cost
>>> print(left_over_money)
Python is an amazing language for solving all kinds of math problems. For basic calculations, you can use:
- Addition: Use the
+
operatornumber_one + number_two
- Subtraction: Use the
-
operatornumber_one - number_two
- Multiplication: Use the
*
operatornumber_one * number_two
- To type the multiplication operator, hold the Shift key and press 8.
- Division: Use the
/
operatornumber_one / number_two
Now take a moment to reflect on the many calculations you can perform with these operators. Knowing how to add, subtract, multiply, and divide gives you numerous ways to solve mathematical problems.
Quiz Time!
Which key combination do we need to press in order to obtain the multiplication operator?
- Shift + x
- Ctl + 8
- Shift + m
- None of the above
Final Note:
Please don’t rush through this session. Learning a new programming language takes time and practice. Feel free to review today’s material, and when you feel ready, continue to the next session.