calculator footprint carbon
TRANSCRIPT
CARBONFOOTPRINT
CALCULATOR
BROUGHT TO YOU BY CODE CLUB AUSTRALIA POWERED BY TELSTRA FOUNDATION
SUBMIT AND BE COUNTED ATMOONHACK.COM
BURMESE
Carbon Calculator
αααα
Our personal contribution to the worldβs carbon emissions is called our Carbon Footprint. If there is
too much carbon being produced the world will continue to heat beyond repair.
αααα¬αα αΌαΈ α αα¬ααααααααΌααΌαΈα α¬αΌαΈαα αα»ααα α ααααα± αα«ααααααααΌααΌαΈα αα»ααα α
Carbon Footprint αα¬αααααΌα±α¬ α ααΌα±α«ααα α‘ααα αα¬ααα α‘ααα‘αα»α»αααααα«α
αααα¬αα αΌαΈαα αααααααααααααα α‘αααΌα‘αααα αα α¬ααα α‘αααααα¬αα«ααα
There are a number of tools online that help us to measure our carbon footprint, which can also help
us to better understand how to lessen our impact on the planet.
αα»ααα α αα¬αααααΌα±α¬α αααΌαΈαα¬αααα αα±αα¬α α¬αΌαΈ α αα«ααα αααΌαΈα αα αααα¬αααα‘ααα±α«
αα»ααα αααα» αααα±α¬ααα ααα αα αα¬αΌαΈαααα¬αα±α±α αα αααΌαΈααααα
What you will make αα αααΌααα±ααα‘α±α¬α
In this project, we will create a simplified carbon footprint calculator that can help people to
understand their personal impact.
ααΌ ααα±α¬α ααα αα‘α α¬αΌαΈα αα α ααααα± αααα±α¬ααααα αα¬αΌαΈαααα¬αα‘α¬α
αα αααΌαΈαααα ααΌαΈα ααΌαΈαααααα¬ αα¬αααααΌα±α¬ααααΌαΈααα±ααα±αΌα αα»ααα ααα αΌαΈα±αα«ααα
What you will learn αα αααα¬α±αα α‘α±α¬α α¬αΌαΈα
β’ Loop through data in a list
β’ Sum values based on user input
β’ Dynamically format strings
β’ Validate input
β’ List αα±αΌα‘αααΌαΈα data α α¬αΌαΈα loop ααααΌααΌαΈα
β’ User input ααα±α« αααα ααααΌαΈα α¬αΌαΈα ααα«ααΌαΈααΌααΌαΈα
β’ α±α¬α αα¬ααΌαΈ(String)α α¬αΌαΈα
What you will need HARDWARE β a computer capable of accessing Trinket online. You do not need a Raspberry Pi to
complete this project.
DOWNLOADS
Starter Code - https://trinket.io/embed/python/d504a518be
Additional notes for educators Here is a link to the completed project - https://trinket.io/python/bd3506101e
Step 1: List the questions and answers α‘αααΌαΈαα‘αααα α¬αΌαΈα List α±α¬α±ααΌαΈαααΌα±ααΌααΌαΈ
First, we want to ask the user some questions, and provide them with the possible answers that will
allow us to measure their carbon footprint.
ααααα»αΌαΈ αα»ααα α‘ααα α‘αα»αΌαΈαααΌαα αααΌαΈαΌααΌαΈα‘αΌ αΌα» αααΌαΈα±αα«αααα ααα¬α αα α ααα±ααααΌ α ααα¬
α‘αααα α¬αΌαΈ ααα¬ααα»αααΌαΈα αΌαΈ αα α± αα¬αααααΌα±α¬ αααΌαΈαα¬α α¬ ααα±αα«ααα
β’ Open the starter project at https://trinket.io/embed/python/d504a518be
β’ ααΌ Link α‘αααΌαΈ ααα₯ αΌαΈααα±α¬α αα αααα« - https://trinket.io/embed/python/d504a518be
β’ On the left is your code window, on the right is the result of your code. You can click the
βRunβ button to run your code. Youβll notice that the first 2 lines have already been written
for you.
β’ ααααΌααΌαΈαα αα αααα±αΌαΈα±ααααα±α¬ααα±αα αΌαΈ αα¬ααΌααΌαΈαα αα ααα±αα¬αα
αααααααα±α¬ααα±ααα βRunβ αΌααα α αα ααααα α‘αααααΌααΌαΈααααα ααααα»αΌαΈ α
α αα¬ααΌαΈα ααα‘αα αα±αΌαΈαα¬αΌαΈαα αΌαΈααα±ααα αααα¬αΌαΈααα«ααααα
The first line tells us to use version 3 of Python, the second gets the data for our carbon
calculator.
ααααα±α αα¬ααΌαΈα Python αα¬αΌαΈα ααΌαΈ α α α‘αα»αΌαΈαααΌααα‘α αα¬ααΌαΈ αααα¬αα¬αΌαΈαα¬αα«α
ααααα±α αα¬ααΌαΈα Carbon calculator ααα αααα¬α α±ααα¬αΌαΈαα¬ ααα±αα«ααα
β’ Our data consists of a series of questions, with each of the answers given a value for how
much carbon weβre using.
β’ αα»αααα¬α α± αααα¬α α¬ αααΌαΈαΌααΌαΈα± αΌαΈα± αΌαΈαα αα«α αα«ααα α‘ααααα±αΌαΌ ααΌαΈα α αα»αααα¬α
α‘αα»αΌαΈαααΌααα» αα¬ααααα¬αα‘αα ααα ααα¬αΌαΈαα αΌαΈ ααα±ααα
β’ Letβs start by listing out all the questions. To do this, weβll write a βforβ loop that will give us
each of the questions in turn:
β’ αααΌαΈαΌααΌαΈα‘α¬αΌαΈαα»αΌαΈα List αααΌααααΌααΌαΈααα α±ααα±αα‘α¬αα αααΌαΈαΌααΌαΈαα±αΌαΌ ααΌαΈα αααα¬ααα βforβ
loop αα±αΌα αα±αΌαΈα±αα«ααα
β’ We want to write out each question. To do this, weβll use the βprintβ command:
β’ αααΌαΈαΌααΌαΈαααΌαΈα α±α¬αα¬αΌαΈαααα¬αα±αΌ ααα¬ ααα±ααα αα« α‘ααα αα¬α βprintβ command α
α‘αα»αΌαΈαααΌα±αα«ααα
β’ Run your program using the βRunβ button. In your Result window, you should see a list of
questions.
β’ βRunβ αΌααα α ααα αΌαΈ ααα± αααα±αα α‘αααΌααΌαΈαααα‘αΌα«α α¬ α±αα¬ααααα±α¬ααα±α¬α α¬
αααΌαΈαΌααΌαΈ List α αααα±α α¬αα«α
β’ For each question, we want to list out the answers. Add a second loop that outputs all the
possible answers to the question:
β’ αααΌαΈαΌααΌαΈαα±αΌαΌ ααΌαΈα α‘αα α‘αααα α¬αΌαΈα list αααΌαααΌ ααα«ααα αα«α αα¬α ααα±ααααΌ α α
α‘αααα α¬αΌαΈα‘α¬αΌαΈαα»αΌαΈα αααΌαΈαΌααΌαΈαααΌαΈα α¬ αααα±α αααα‘ααα loop αα±αΌ ααααα±αα«ααα
β’ We want to number our answers to give the user something easy to type in. First, we need
to set an initial value for our number:
β’ α‘αααα α¬αΌαΈα αα»αα«αα±α₯ααΌααΌαΈα‘α¬αΌαΈααα α‘αα»αΌαΈαααΌαα‘ααα αα±αΌαΈαΌ αα± ααααα±αΌ ααα«ααα
αα«α αα¬α αα»αααα¬α αα»αα«αα‘αα ααα₯ αΌαΈααααΌαΈ α ααα αα ααα«ααα
When programming we often use the letter βiβ to indicate a counting number.
αααα±αΌαΈαα‘αΌα«α α¬ i α±α¬αα»αΌαΈα αα±ααααααΌαΈα‘ααα± α αα¬αΌα α‘αα»αΌαΈαααΌαα α αα«ααα
β’ Next, we need to increase the value of βiβ every time we go around our answer for loop:
β’ α ααα¬α αα»αααα¬α α‘αααα‘αα Loop αααααΌαΈ i ααααΌαΈα ααΌαΈαα¬αΌαΈαα±α ααα«ααα
β’ Now we can add this to the text of our answer:
β’ α i ααααΌαΈα αα»αααα¬α α‘ααα α±α¬α αα¬ααΌαΈα‘αααΌαΈα α¬ ααααα«ααΌαΈαααααα«ααα
β’ Run your program. You should now get all of the questions with numbered answers.
β’ αααααα±αα run αααα«α αααΌαΈαΌααΌαΈα α¬αΌαΈα‘α¬αΌαΈαα»αΌαΈα‘αα αα»αα«αα±α₯α‘ααα‘αααα α¬αΌαΈα α±α α α¬
ααα±αα«ααα
Step 2: Totalling it all up Next, we will get all the userβs responses and total up their emissions.
αα»αααα¬α α‘ααα α‘αα»αΌαΈαααΌααα»α αα ααααΌ αα‘α¬αΌαΈαα»αΌαΈα α±ααα αΌαΈ αα α± (αα¬αα)αααααααα¬α
α α±α±ααα«ααΌαΈα±αα«ααα
β’ Now that weβre asking the user a question, we need to get their answer. Add the following
code to ask the user for their answer and store it in the variable named βresponseβ:
β’ ααΌ αα»αααα¬α α α‘αα»αΌαΈαααΌααα» αααΌαΈαΌααΌαΈαααΌαΈαα αΌαΈααααα¬α α‘αααα αααΌα»α±α
αα‘ααα«ααα α‘αα»αΌαΈαααΌα α α‘αααα αααΌαΈααααΌαΈαα αΌαΈ βresponseβ ααα variable α‘αααΌαΈ
αααΌαΈαααΌαΈαα code α αα‘α¬ααα«α‘αααΌαΈ αααααα«α
β’ For the question, we want to get the carbon amount from the relevant answer. Add the
code to print out the amount of carbon:
β’ α‘αααα ααα αα¬ααααα¬αα α±α αΌ ααα«ααα αα¬ααααα¬α α print αααααΌαΈα±α αα‘α¬ααα«
code α αααααα«α
β’ Run your program. What happens when you try and put in a response? You probably get an
error saying:
β’ αααα±α α run αααα‘αΌα«α α¬ ααα ααα¬ αα αααα α±α αα«αα α ααα‘ααα
αα‘α¬ααα«α‘αααΌαΈ error α±ααα¬ααΌαΈα±αααα«ααααα
β’ The reason we get this error is because the input command gives us a string (text) instead of
an integer (number). We need to convert our response into a number before we can use it:
β’ α‘ααα« error α±α α α‘α αα¬ααΌαΈα±ααΌαΈα input command α integer (ααααΌαΈ) α‘α±α¬αΌαΈ string
(α±α¬αα¬αΌαΈ) α αααααΌαΈααα¬α αα¬α ααα±ααα αα»αααα¬α α‘ααα α‘αααα α‘αα»αΌαΈααααΌαΌα
number(ααααΌαΈ)α‘ααα± αααα¬ααΌαΈαα ααα«ααα
β’ Run your program again. Thatβs better.
β’ αααα±αα αααα» run αα«α ααααα¬ααΌαΈααα α±αα α±αα«ααααα
β’ Instead of showing the amount of carbon after each step, we want to keep a running tally
and display it after weβve asked all the questions. First, we need to initialise our total to 0
before we start our question loop:
β’ αα¬ααααα¬αα ααααΌ ααΌαΈα αααΌαΈ ααααΌ ααΌαΈαα α±α±ααα«ααΌαΈα‘ααααα αααΌαΈαααΌαΈαα¬αΌαΈαα αΌαΈ
αααΌαΈαΌααΌαΈα α¬αΌαΈα‘α¬αΌαΈαα»αΌαΈα αααΌαΈαα αΌαΈααα¬α αααααα«ααα ααααα»αΌαΈ αα»αααα¬α α‘ααα αααΌαΈαΌααΌαΈ
Loop α αα±ααα α±α±ααα«ααΌαΈααααΌαΈ α αα(0) α‘ααα± ααα αα±αααα«ααα
β’ Instead of printing out the amount of carbon used after each question, we want to add it to
the total:
β’ αααΌαΈαΌααΌαΈααΌαΌ ααΌαΈα α‘αα αΌαΈα α¬ αα¬ααααα¬α α αααααα‘α±α¬αΌαΈ αααΌαΈααααΌαΈα α±α±ααα«ααΌαΈααααΌαΈ
(total) αα ααα«ααΌαΈααα±αα«ααα
β’ Finally, we want to print this out after weβve finished looping through the questions:
β’ ααα¬ααα»αΌαΈα‘α¬αΌαΈααα αααΌαΈαΌααΌαΈα α¬αΌαΈα‘α¬αΌαΈαα»αΌαΈαααΌαΈαα αΌαΈααααα¬α αααΌαΈα±α±ααα«ααΌαΈααααΌαΈα ααααα«ααα
Note that we donβt have any spaces in front of line 16. The spaces are called βindentingβ and
they tell Python that a command is in the loop. We donβt want our print statement to be in
the loop, so we donβt want it to be indented. Make sure this code is right against the edge.
α±α¬α αα¬ααΌαΈαα»αα«α αα αα αααα space (αααα) α α±α¬α αα¬ααΌαΈα‘αα α»αα α αααααα
αααααΌαα«α α‘ααα« space (ααααα α¬αΌαΈ)α βindentingβ α ααΌα±α«ααα±α« αα αΌαΈ αααΌαΈα α command
α±α¬α αα¬ααΌαΈαα±αΌαα Loop α‘αααΌαΈα αα α Python α‘α¬αΌαΈ αααα¬αααα«ααα αα»αααα¬α α‘ααα
print α±α¬α αα¬ααΌαΈα Loop α‘αααΌαΈα α¬ α ααααα±αΌ ααα‘αα αααΌαΈα indent αααΌααα±ααααα« α
α‘ααα« code αα α‘α±ααΌαΈα α ααα αα ααα αααΌ α¬αα«αα±α
β’ Run your program. You should be able to answer all of the questions and get an answer at
the end.
β’ ααα αααα±αα run αα«α ααα‘ααα αααΌαΈαΌααΌαΈα α¬αΌαΈα‘α¬αΌαΈαα»αΌαΈα ααααααα α¬ ααα±αα αΌαΈ α‘αα»αΌαΈα α¬
α‘ααααα±αΌ α±α αα«ααααα
β’ Currently, the program is just spitting out a number. Letβs make that number a bit more user
friendly. Update your print line to the following to give your user some context to the
number:
β’ ααΌ αααα±αα‘ααα ααααΌαΈα±ααααα¬ αααααΌαΈαα«αααΌαΈααα αα«α αα¬α α α±ααα
α‘αα»αΌαΈαααΌαα‘αα αα‘ααααααα‘α¬αααααααααΌαΈ αα± α ααα± print α±α¬α αα¬ααΌαΈα αα‘α¬ααα«
code α‘αααΌαΈ ααααΌαΈα‘ααα α‘ααΌα¬αΌαΈααα¬α±αα¬αΌαΈα±α α¬αΌαΈ αααααΌαΈαααΌαΈα±α update αααΌαααα«α
β’ Run your program again. You should now get the total CO2 emitted displayed in a readable
format.
β’ αααααα±αα ααα¬ααααα run αα«α ααα‘ααααα α±α±ααα«ααΌαΈ CO2 αααααααα¬α α
αααα±αααααα¬ αα»α±α»ααα αααα±αα«ααααα
Step 3: Input validation You have a working CO2 calculator, but what happens if the user enters a number that the program
isnβt expecting, or something that isnβt a number at all? Did you see an error called βIndexErrorβ? In
the next step weβll validate the user input so that if they enter something incorrect, it doesnβt crash
the whole program.
ααα α¬ α‘αααααααα ααα±α CO2 calculator αα±αΌ α αααα«αα ααα«αααα α‘αα»αΌαΈαααΌαα αααα±αα
αααΌα»ααα¬αΌαΈααα¬ αααΌαΈααααΌαΈαα±αΌαΌ(α ) αααΌαΈααααΌαΈαααααα±α¬α αα¬ααΌαΈαα±αΌαΌα
αααααΌαΈααα¬α‘αΌα« αααα α αΌαΌαΈ ααα±αα¬αΌαΈαα«ααα βIndexErrorβ α ααΌα±α«ααα¬ αααΏαα¬α αΌαΌαΈα
αααααα»α±αα«ααα¬αΌαΈα ααα¬ααα±αααα α α¬αΌαΈαααΌαΈααα¬α‘α±α¬αα±αΌαΌ αααααΌαΈαα«α αααα±ααα±αΌαα»αΌαΈα
αααΏαα¬αααα±αα¬αΌαΈαα±α±α αα»αααα¬α α‘ααα α‘αα»αΌαΈαααΌαα αααααΌαΈαα α± α±α±α±αα«ααααα
β’ The first step in validation is to tell the user what is expected. Letβs tell our user what is
expected of them when you ask them for their answer:
β’ α± α±α±α±α¬α α¬ αααα‘ααα‘ααα α‘αα»αΌαΈαααΌαα αα¬αα ααααααΌαΈα ααα»α¬αααα¬αΌαΈααααα¬
αααα¬α ααα«ααα αα«α αα¬α αα α α α‘αααα αααΌαΈαα‘αΌα« α‘αα»αΌαΈαααΌαα αα¬αα
ααα»α¬αααα¬αΌαΈααααα¬ αααΌαααααα¬α±αα«ααα
The βformatβ command might be a little confusing, but for now all you need to know is that it
replaces β{}β with the number stored in βiβ.
βformatβ command α α‘αααΌαΈαα αα¬αΌαΈααα±αΌααα«ααααα αα«αααα β{}β αααΌαΈα ααααΌαΈα
βiβ ααααΌαΈα α‘α±α¬αΌαΈααΌαΈα α±α¬ α‘αα»αΌαΈαααΌααα α α‘αΌ ααα¬αΌαΈα±α α‘ααααααα«ααα
β’ Run your program. You might notice that the range given is one larger than the number of
options. Oh no! We can fix this substituting βi-1β instead of βiβ in format:
β’ αα αααα±αα run αα«α αααΌαΈαα¬αΌαΈα range α‘αααΌαΈα α¬ αα± αααα ααααΌαΈαα±αΌαΈαΌ αα
ααα±αααα¬α ααα»α±αα«ααααα αα«α αααα ααΌαΈα α‘αα format ααα±α¬α α¬ βIβ α‘α±α¬αΌαΈ βi-1β α
α‘α±α¬αΌαΈααΌαΈα±αα«ααα
β’ Run your program again. Thatβs better!
β’ αααα±αα αααα» run αα« α ααααα¬ααΌαΈαααα¬αα¬ ααα»α±αα«ααααα
β’ Now weβre telling the user what to enter, they should get it right, but just because they
should get it right, it doesnβt mean they will! Everyone makes mistakes, and some people
will deliberately try and break programs (thatβs one of the ways that hackers can break into
computers). We still need to check that the input is valid.
β’ ααΌ αα»αααα¬α α α‘αα»αΌαΈαααΌα α αα¬αααααΌαΈα±ααααα¬ αααα¬αααα‘αααΌαΈ
α‘αα»αΌαΈαααΌαα‘ααα α αα ααααα αααααΌαΈαααα«ααα αα«ααααα α±αααΌ α±αααΌαΈαα«ααΌαΈ α
ααααΌαΈα α‘α α¬αΌαΈαα αααααα«ααα α‘αΌ αΌα»αααα αα±αα±αα α‘α α¬αΌαΈαααΌααααα αΌαΈ αααα±αα
α αα± αΌαΈααα¬α αΌαΌαΈαα α αααα«ααα αα«α αα¬α αα»αααα¬α α‘ααα αααααΌαΈα input α
α αααααΌααΌαΈα /αα α±α±αααΌαΈα αα‘ααα«ααα
β’ Letβs start by checking that a number is actually entered before we tell python to convert it
into a number:
β’ αα»αααα¬α α‘ααα input α ααααΌαΈα‘ααα± ααααα¬ααΌαΈαα α‘ααα« ααααΌαΈα α‘α αααα
αααααΌαΈααΌααΌαΈα /αα α α±ααα±α±αααΌαΈα±αα‘α¬αα
Note that weβve indented line 14 to the next level to tell Python that itβs inside the if
statement.
α±α¬α αα¬ααΌαΈαα»αα«α αα α α¬ if statement α‘αααΌαΈ α α αα¬ααΌαΈ ααα¬α±α indent
(ααααααΌα¬αΌαΈαα¬αΌαΈαα) α αααααΌαα«α
β’ If the user enters something that isnβt a digit, we should tell them that they need to enter a
number:
β’ α‘αααα±α» α‘αα»αΌαΈαααΌαα‘ααααα ααααΌαΈ αααααΌαΈααΌααΌαΈ ααααΌαα«α αα»αααα¬α α‘ααααα
ααααΌαΈααα¬ αααααΌαΈα αα αα¬ααΌαΈ αα‘α¬ααα«α‘αααΌαΈ α‘ααααΌαΈα±αα«ααα
β’ Next, we need to validate the number, to ensure that itβs in the right range, only then will
we add the number to the total. Add the βifβ below, and indent the existing βtotal +=β line:
β’ α ααα¬α αα»αααα¬α α‘ααα αααααΌαΈααα¬ ααααΌαΈαα α αααααα¬ range α‘αααΌαΈαα
α ααααα α±α±αααΌαΈα±ααααα ααααα¬αα α‘ααα« ααααΌαΈα α±α±ααα«ααΌαΈααααΌαΈ total αα
ααα«ααΌαΈααααα αα‘α¬ααααααα¬αΌαΈααα‘αααΌαΈ βifβ ααα¬ααααα±α¬α αα¬ααΌαΈα α¬αΌαΈα αααα«α
βtotal +=β α±α¬α αα¬ααΌαΈαα indent αααΌαααα«α
β’ Run your program. What happens if the user enters something wrong? It doesnβt break the
program anymore, but it just goes on to ask the next question, and so wonβt give an accurate
result. We want to ask the user the question again and again until they give a valid response.
β’ ααα αααα±αα run αα« α α‘ααα α‘αα»αΌαΈαααΌαα α‘α α¬αΌαΈαα±αΌαααα«α ααα ααα±αα«αα?
αααΌαΈαα αααα±αα αα αα± αΌαΈααα¬ααα¬αααΌαΈ ααα¬ααααΌαΈαΌααΌαΈααα¬
αααααααΌαΈααααα±ααα¬α αα¬α α αααααα¬α‘ααα α±α αα ααααα«α α α αα¬α
αα»αααα¬α αα α‘αα»αΌαΈαααΌαα‘ααααα α αααααα¬ αααααΌαΈα αααα±ααΌ ααΌαΈ
α‘ααα«αααΌαΈαΌααΌαΈααα¬ αααΌα«αααΌα« αααΌαΈααααΌαΈα±ααα‘αααα
β’ To achieve this, weβll use a while loop. This will keep looping through until the condition is
satisfied (in this case, we get a valid response). Start by initialising a Boolean variable:
β’ αα ααα±αα±α±α αα»αααα¬α α‘ααααα while loop α α‘αα»αΌαΈαααΌα±ααααα
α αααααα¬α‘αααΌα‘αααααα±ααΌ ααΌαΈ (α αααααα¬ αααααΌαΈα αααα±ααΌ ααΌαΈ) αααΌαΈαα
αααΌα«αααΌα« looping ααααααααα Boolean variable (α‘α α¬αΌαΈ/α‘α αα± α±α±ααα¬ variable) ααα
α±αααα«α
We will assume a response is not valid until weβve checked everything required to tell us
that it is.
αα‘αααα α¬αΌαΈα‘α¬αΌαΈαα»αΌαΈα α± α±α±αααα αΌαΈααΌ ααΌαΈ αααααΌαΈα α α α¬αΌαΈαααΌαΈααα ααα₯ αΌαΈ
αααα¬αΌαΈαα«ααα
β’ Our while loop will run all of our validations:
β’ While loop α‘αααΌαΈαα αα»αααα¬α α α±α±αααΌαΈαα α¬αΌαΈα‘α¬αΌαΈαα»αΌαΈα run αα«ααα
Note that weβve indented everything up to line 21 to another level. You can do this by
highlighting all of the code between lines 14-21 that you want to indent and pressing the
TAB key.
α±α¬α αα¬ααΌαΈαα»αα«α αα α‘α α‘α¬αΌαΈαα»αΌαΈα indent αααΌαααα¬αΌαΈα±ααα‘αααα α±α¬α αα¬ααΌαΈ αα α αα
α‘αααΌαΈα code α α¬αΌαΈα‘α¬αΌαΈαα»αΌαΈα select αααΌαααα¬αΌαΈαα«α α‘ααα«α±α¬α αα¬ααΌαΈα α¬αΌαΈαα highlight
ααα±ααααααα αα‘αΌα« indent αααΌααα±αα‘αα TAB key α α ααα«α
β’ If you run your program now, you will be stuck in an endless loop, because we never set
valid to True. Letβs do that if our number is between the correct range:
β’ ααα‘ααααα αααα±αα ααΌ run αα«α α‘αα»αΌαΈαααααααα¬ loop α‘αααΌαΈαα
αααααααααα α‘ααα αα¬ααααα¬ αα»αααα¬α αα valid α True α‘ααα±
αααα ααΌααα¬α αα¬α ααα±ααα α α αα¬α αα»αααα¬α α αααααΌαΈα ααααΌαΈαα
α αααααα¬ range α‘αααΌαΈαα α αα«α valid α True α‘ααα± ααα ααα«α
Note that weβve moved line 23 out of the if statement and the while loop by decreasing the
indent by two levels. We did this by using the backspace at the beginning of the line.
α±α¬α αα¬ααΌαΈα‘α α αα α if statement α α while loop α α α‘αααααα indent level ααΌ
ααα¬αα αα±α» ααΌααΌαΈααα αααα¬ααΌαΈααα¬αΌαΈααα α±α¬α αα¬ααΌαΈα‘α±αα backspace α αααΌααΌαΈααα ααα¬αα
αα±α»αα«ααα
β’ If the user enters a number outside of the correct range, we should tell them what theyβve
done wrong:
β’ α‘ααα α‘αα»αΌαΈαααΌαα‘ααααα α αααααα¬ range α‘αααααα ααααΌαΈαα±αΌα ααααα«α
αα»αααα¬α α‘ααααα α α¬αΌαΈαααΌαΈααα αα¬ααΌαΈ ααα¬ααα±αα«ααα
β’ Run your program. It should now be working and correctly validating user responses.
β’ ααα± αααα±αα run αα« α αααΌαΈαα α‘αα»αΌαΈαααΌαα αααααΌαΈαα α¬αΌαΈα‘ααα±α« α αααα±α¬
α± α±α±αα αΌαΈααα¬α ααα¬ααΌαΈα±α¬ α‘αααααααα«ααααα
Congratulations- ααααααΌαΈαααα¬αααΌααΌαΈ
You have finished the project. You can try one of our other projects or continue with the challenges
below.
ααα‘ααααα ααα±α¬α αα αα‘α¬ααααα±α¬ αα αΌαΈαα»αΌαΈαα¬αΌαΈαα«αα α α‘ααΌα¬αΌαΈααα¬ααα±α¬α αα α¬αΌαΈα
αααααααΌαΌαΈα±α¬αΌαΈαααα«αα (α ααα) αα‘α¬αααααα¬αααα¬αΌαΈαα α±αααΌα±α«αα α¬αΌαΈα
αααααααα ααΌαΈααααα
Donβt forget to talk with an adult about registering for Moonhack and downloading your certificate
at Moonhack.com
Moonhack α‘αα register α±α¬α±ααΌαΈαααΌαΈαααΌαΈα±α ααα α‘ααααΌαΈαα αααα¬α ααααα«α α ααα‘αα
αααααΌααα αα Moonhack.com α ααα ααα«ααΌαΈααα±ααααα«ααα
Challenge: On repeat - α±αααΌα±α«α
Can you loop through the entire program so that multiple people can use the carbon calculator
without having to reset it every time?
α‘αΌ ααααΌαΈ αααα±αα reset αΌ α±α ααα α‘αα»αΌαΈαααΌαα α¬αΌαΈ(α±α¬) α αα¬αα calculator α
ααααα‘αα»αΌαΈαααΌααα±α ααα‘ααααα αααα±ααα±αΌαα»αΌαΈα loop αααααα«ααα¬αΌαΈα
Advanced Challenge: Graph the result Can you graph the carbon footprints of your classmates? If you want to graph it in Python, check out
the Popular Pets Code Club project.
ααα α‘αααΌαΈααα¬α α¬αΌαΈα± αα¬αααααΌα±α¬ (carbon footprint) α α¬αΌαΈα graph αα»ααα¬αααα«ααα¬αΌαΈ? Python
αα graph αα»αααΌαααΌ ααα«α Popular Pets Code Club project α ααααα«α