Course Overview | Previous Session | Next Session
Introduction
I love playing with Sam and am amazed at how quickly he grows. Mom told me that dogs age much faster than humans do. Dogs age roughly seven years for every human year. Sam is about a year old, and I’m curious how old he will be in four years from now.
Let’s write a program to calculate a dog’s age in human years using the formula:dog_years * 7 = human_years
.
>>> dog_age = 5
>>> human_years = dog_age * 7
>>> print(human_years)
Explanation
- Variable Declaration
We declare two variables:dog_age
andhuman_years
. - Assigning Values
We assign the value5
(representing Sam’s age in 4 years from now) todog_age
. - Calculation
We multiply dog_age by 7 to convert the dog’s age to human years. - Output
Finally, we display the result using theprint()
function.
What to Expect
If you run the code, you should see the result 35
, which is Sam’s age in human years when he is 5 years old.
Practice Exercise: Vet Cost Calculator
Every two years, we bring Sam to the vet for a check-up. The vet bill is $129.00 for the first two check-ups, $149.00 for the next two, and $180.00 for the fifth visit. We need to calculate the total vet cost for the next 10 years.
Let’s write a program to calculate this:
- First checkup: $129.00
- Second checkup: $129.00
- Third checkup: $149.00
- Fourth checkup: $149.00
- Fifth checkup: $180.00
>>> visits_1and2 = 129.00 * 2
>>> visit3and4 = 149.00 * 2
>>> fifth_visit = 180.00
>>> total_vet_cost = visits_1and2 + visit3and4 + fifth_visit
>>> print(total_vet_cost)
Explanation
- Variable Declaration
- We create variables to hold the cost of each set of vet visits: visits_1and2, visit3and4, and fifth_visit.
- Calculation
- We calculate the total cost by multiplying the cost of each visit by the number of visits and then summing them up.
- Output
- We use print(total_vet_cost) to display the total cost for the next 10 years.
What to Expect
If you enter the code correctly, the output should be $737.00
. This is the total cost for Sam’s vet check-ups over the next 10 years.
Take Your Time
As you go through this code, take your time to understand each part. It’s perfectly okay to scroll up and review previous examples for reference. Practice makes perfect!
Quiz
Instead of calculating the cost of bringing Sam to the vet every two years, let’s calculate the cost of filling up Mom’s car twice a month for a whole year. Each time she buys gas, she pays $80. Write a Python program to find out the total cost for gasoline for a whole year.
Remember, the variable names you choose are a personal choice and can be different from those shown here. What’s most important is that your code calculates the correct result. As long as your variables represent the correct values and you get the right answer, you’ve done it right!
Important Note
You may have noticed that every time we exit the Python interpreter, all of our code is lost. This happens because the Python interpreter is a temporary environment that only stores our code while we’re working on it. Once we exit, all progress is gone!
“But don’t worry! Starting in the next session, we’ll learn how to write our code in a text editor so we can save it. This way, we can keep our code organized and run it whenever we want!”