calculator footprint carbon

12
CARBON FOOTPRINT CALCULATOR BROUGHT TO YOU BY CODE CLUB AUSTRALIA POWERED BY TELSTRA FOUNDATION SUBMIT AND BE COUNTED AT MOONHACK.COM BURMESE

Upload: others

Post on 16-Oct-2021

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CALCULATOR FOOTPRINT CARBON

CARBONFOOTPRINT

CALCULATOR

BROUGHT TO YOU BY CODE CLUB AUSTRALIA POWERED BY TELSTRA FOUNDATION

SUBMIT AND BE COUNTED ATMOONHACK.COM

BURMESE

Page 2: CALCULATOR FOOTPRINT CARBON

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.

α€šα€Ό ပရောဂ ကတင α€œα€‘α€™ ာြးက α€žα€ ၏ α€α€€α€šα€›α€± α€žα€€α€›α€±α€¬α€€α€”α€„α€™α€€ α€”α€¬α€Όα€Έα€œα€Šα€œα€¬α€›α€‘α€¬α€„

α€€α€Š α€›α€•α€Όα€Έα€”α€„α€žα€Š α€›α€Όα€Έα€› α€„α€Όα€Έα€œα€šα€€α€›α€žα€¬ ကာဗနရခြောဂဏနြးတကေကတေြက ကျနပတ ဖနတ α€Όα€Έα€±α€•α€«α€™α€šα‹

Page 3: CALCULATOR FOOTPRINT CARBON

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” α€Όα€œα€α€€ α€” ပ၍ α€žα€„α€€α€’α€€ α€‘α€œα€•α€œα€•α€Όα€„α€Όα€Έα€”α€„α€žα€Šα‹ ပထမဆျြး ၂

α€› ကာငြးက α€žα€„α€‘α€α€€ ရေြးထာြးပပ α€Όα€Έα€α€–α€±α€žα€Šα€€ α€žα€α€‘α€¬α€Όα€Έα€™α€•α€«α€œα€™α€™α€šα‹

Page 4: CALCULATOR FOOTPRINT CARBON

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:

Page 5: CALCULATOR FOOTPRINT CARBON

β€’ ရမြးြနြးတေြြ ငြးဆ ထတက ထရခဖမ ာြးက 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”:

Page 6: CALCULATOR FOOTPRINT CARBON

β€’ α€šα€Ό ကျနရတာတ α€€ α€‘α€žα€»α€Όα€Έα€α€•α€Όα€žα€‘α€» ရမြးြနြးရမြးပပ α€Όα€Έα€žα€Šα€›α€”α€¬α€€ ထရခဖက α€œα€€α€Όα€»α€±α€”

α€œα€‘α€•α€•α€«α€α€šα‹ α€‘α€žα€»α€Όα€Έα€α€•α€Όα€ž ၏ ထရခဖက ရမြးခမနြးပပ α€Όα€Έ β€œ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:

β€’ ကာဗနပမာဏက တဆငြ ငြးဆ တငြး α€α€•α€žα€Ό ငြးထက ေေရပါငြးထရနခဖင α€žα€™α€Όα€Έα€†α€Šα€Όα€Έα€‘α€¬α€Όα€Έα€•α€• α€Όα€Έ

α€›α€™α€Όα€Έα€Όα€”α€Όα€Έα€™ α€¬α€Όα€Έα€‘α€¬α€Όα€Έα€œα€»α€Όα€Έα€€ ရမြးပပ ြးရနာက α€α€•α€žα€œα€•α€«α€žα€Šα‹ ပထမဆျြး ကျနရတာတ ထရနန α€›α€™α€Όα€Έα€Όα€”α€Όα€Έ

Page 7: CALCULATOR FOOTPRINT CARBON

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 ောရ ကာငြးက ရထာကပါ

Page 8: CALCULATOR FOOTPRINT CARBON

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” α€€

Page 9: CALCULATOR FOOTPRINT CARBON

α€‘α€±α€¬α€Όα€Έα€‘α€Όα€Έα€±α€•α€«α€™α€šα‹

β€’ 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 α€‘α€ž

Page 10: CALCULATOR FOOTPRINT CARBON

α€›α€•α€«α€„α€Όα€Έα€‘α€Šα€™α€Šα‹ α€›α€‘α€¬α€€α€α€„α€α€•α€‘α€¬α€Όα€Έα€žα€Šα€‘α€α€„α€Όα€Έ β€˜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:

Page 11: CALCULATOR FOOTPRINT CARBON

β€’ 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:

Page 12: CALCULATOR FOOTPRINT CARBON

β€’ α€‘α€€α€šα α€‘α€žα€»α€Όα€Έα€α€•α€Όα€žα€‘α€›α€”α€α€–α€„ α€™ α€”α€€α€”α€›α€žα€¬ 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 α€€ α€€α€Šα€›α€•α€«α‹