CALCULATOR FOOTPRINT CARBON
Transcript of CALCULATOR FOOTPRINT CARBON
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 แ แแแแแซแ