SlideShare a Scribd company logo
1
Coding in Disaster Relief
Situation Report:
Introduction:
Welcome to our village, engineering team.
Your task is to learn the Python language and use it program a transmitter module to send a
distress signal so the village can be evacuated before the hurricane arrives. Please read this
worksheet carefully; you will need to use everything you will learn to save these people.
Good luck.
Explanation (Functions and Arguments):
A function is an operation in code that
performs an action if you give it an argument
(or input).
Let’s try using the print() function to display
a message.
Advanced Commenting & Printing:
People write comments to explain code, if it’s
not easy to understand. It doesn’t get read by
the processor.
In Python, comments are marked by a hash #
symbol.
When printing, you need to put the argument
in quotation marks (“”). It is then called an
input of type String.
Advanced Task:
Can you use the print function to evaluate
mathematical expressions? Try finding the
answer to
8+3.2
8
× 10 using code. (See hint)
Code (Python):
# Printing “Hello World” will send a
# message to the console!
# (print) is the function,
# (“Hello World”) is the argument.
print(“Please send help.”)
>>> Please send help.
# Only write the first line in Python;
# this is called the input.
# The second line starting with “>>>”
# is what you should see after you run
# the code.
# You can press Shift-Enter to get the
# computer to run the code.
# More types of input data
integer = 82
float = 37.462
string = ‘# of villagers, avg age.’
boolean = True
array = [0, 1, 2, 1, 3, 1, 2, 1, 0]
# Hint: check this out!
print((0.4 + 1.8) * 2)
Remember:
1. Use parentheses ( ) properly! Functions always have a pair of parentheses after them,
inside which arguments go.
2. Use quotation marks “” or ‘’ properly! Strings always need to be surrounded by these
when printing, but it doesn’t matter which one – so long as it’s the same.
3. Python is caSE-SenSiTiVE! Make sure you use proper casing when writing code.
2
Coding in Disaster Relief
Coding Basics:
Explanation (Variables):
A variable is a name given to a stored piece of
data. Variables can store numbers, strings, or
Boolean (True/False). Referring to that
variable will then lead you to the data stored.
Explanation (Statements & Conditions):
A statement is a way to make a decision. The
code inside an IF block, for example, will only
run if the condition is true.
The condition is the comparison, or check,
you use in the statement.
Advanced Conditions:
IF-ELIF-ELSE statements allow for much
more complicated decisions to be made.
Advanced Task:
Can you write a series of IF-ELIF-ELSE
statements that returns the name for the
shape with a certain number of sides?
Make sure it can handle shapes with 3 sides
up to 8 sides (an octagon).
Code (Python):
# Variables can even store one another!
volunteers = 8
students = 24
dist = students / volunteers
print(dist, " students per volunteer.")
>>> 3 students per volunteer.
# First, we set the variable.
heavy_weather_warning = True
# Then, we use an if statement.
if heavy_weather_warning is True:
print(“Take an umbrella!”)
>>> Take an umbrella!
# Hint: Check this out!
n_sides = 4
if n_sides == 3:
print(“Triangle”)
elif n_sides == 4:
print(“Square”)
...
>>> Square
Remember:
1. Use indentation properly! Code inside IF-ELIF-ELSE statements always needs to be
indented.
2. Any sort of statement needs a colon (:) after its conditions to end it.
3. Press Shift-Enter to execute (run) the code when you’re done.
3
Coding in Disaster Relief
Coding Basics:
Explanation (Loops):
Sometimes you want code to repeat. Loops
are a special sort of statement that can do that
by setting conditions.
There are two main ways to do this. The first
is using a FOR loop. You get to choose the
number of times the code inside the FOR loop
repeats.
Note that loop counts always start from 0, not
1, which is why we add the +1 in the loop
condition.
The other type of loop is a WHILE loop. Here,
the code will keep looping until a condition is
met that will cause the loop to stop.
This is useful if you don’t know the exact
number of times you’d like to loop the code.
Advanced (Loops):
Loops can be placed inside other loops. For
each repetition in an outside FOR loop, all the
repetitions of an inside FOR loop will be run.
Advanced Task:
Can you write a loop to print out all the
multiplication tables from 1 to 9?
Code (Python):
# This is a for loop.
nHomes = 30
for i in range(nHomes + 1):
print(“People: ” + str(i * 5))
>>> People: 0
>>> People: 5
>>> People: 10
...
>>> People: 150
# Beware! While loops will usually
# require external variables!
nNeed = 150
nWater = 80
# This is a while loop.
# while the number of people needing
# water is greater than 0, loop code:
while nNeed > 0:
print(str(nNeed) + “ need water.”)
if nWater > 0:
# if we have water, distribute.
if nNeed <= nWater:
nNeed = nNeed – nWater
print(str(nWater) + “ ...
given.”)
nWater = 0
else:
nWater = nWater - nNeed
print(str(nNeed) + “ ...
given.”)
elif nWater == 0:
# if we don’t have water, call
# in a supply helicopter
nWater = 80
print(“Supplies requested.”)
# if no-one needs water, the loop ends.
print(“Everyone now has enough water!”)
>>> 150 need water.
>>> 80 given.
>>> 70 need water.
>>> Supplies requested.
>>> 70 need water.
>>> 80 given.
>>> Everyone now has enough water!
# Hint: complete the following code.
# remember, you can print variables!
for i in range(1, 9):
for j in range(...):
print(“...”)
Remember:
1. Just like IF-ELIF-ELSE statements, loops require code to be indented properly!
2. When working with a WHILE loop, you need to set a condition where the loop can end,
otherwise it’ll keep going forever!
4
3. Press Shift-Enter to execute (run) the code when you’re
done.
Coding in Disaster Relief
Miscellaneous Code:
And you’re almost done with the basics!
There’s just a few final things we need to do
before you can code your beacon.
Explanation (Importing):
Python has a number of built-in functions
(print(), int(), etc) you can use to perform
actions. However, sometimes the functions
we want to use aren’t built-in. We will need
to either define them ourselves, or import
functions from other collections, called
libraries.
The time library gives us access to many
functions to do with date and time, including
sleep(), which is used for delaying code.
Explanation (Delays):
Code runs really fast, depending on the
processor. Usually, each line of code takes
less than a thousandth of a second to run!
If we want to do something like flash a light
every second, we need to add a delay to the
code using the sleep() function.
Custom Functions:
What if we didn’t have a built-in function,
and couldn’t find it in any library?
We’d have to make our own function!
Advanced Task:
Can you write a function that takes a number,
and returns the number of factors that
number has?
Code (Python):
# We want to import the sleep()
# function from the time library.
from time import sleep
# This is how a sleep timer works.
# The sleep() function is part of the
# time library.
timer = 5
print(“Flare lit, stand back!”)
while timer > 0:
print(timer)
sleep(1)
timer = timer – 1
print(“Emergency flare fired!”)
>>> Flare lit, stand back!
>>> 5
>>> 4
>>> 3
>>> 2
>>> 1
>>> Emergency flare fired!
# This is how functions are defined.
# The first line defines the name,
# and the inputs it should have.
def isEven(n):
if n % 2 == 0:
# if the remainder of n ÷ 2 is 0
return True
else:
# if n ÷ 2 gives a remainder
return False
# Hint: complete the following code.
import math
def nFactors(n):
factor = 0
count = 1
half = math.floor(n / 2)
while count < half:
...
return factorcount
Remember:
1. Just like decision and loop statements, functions require code to be indented properly!
5
2. Be sure to include a return statement, so that the function
can properly give you an answer.
3. Press Shift-Enter to execute (run) the code when you’re done.
Coding in Disaster Relief
6
Workshop Activity:
Radio Signals:
You’re now ready to reprogram that
transmitter module, engineering team.
Remember, our goal is to broadcast a
message so that a nearby radio can pick it up,
and send help.
Start with importing the library “ewb”.
We’ll need this to be able to broadcast signals.
How do we import libraries again?
We’ll need to use the code on the right to
start the radio broadcast. The first line starts
the code that allows radio signals to be
broadcast on a frequency, and the second line
transmits a message on that frequency.
What if the radio isn’t turned on when it’s
sent? We’ll want to loop the broadcast so
that it keeps repeating.
What sort of loop should we use? What condition
would make it loop forever?
We should add a delay after the message is
sent inside the loop to make it clearer for
rescuers to hear us.
Did we need to import a library for the delay?
What function added the delay to the code?
And we should be done! Run your code, and
get a volunteer to check it over. Hopefully
you can hear your message over the radio!
Advanced Task:
Can you write code to broadcast the date and
time as well as the location in the message?
Code:
# Replace the ‘...’ with your frequency
radio = ewb.Radio(...)
# Replace the ‘...’ with a message
radio.say(“...”)
# You might need these
# following snippets of code:
import datetime
current = time.strftime(“%c”)
Remember:
Check the previous pages of the worksheet if you need any help!
7
Raspberry Pi Setup:
1. Disconnect the laptop from the internet:
2. Insert the SD Card into the Raspberry Pi:
3. Gently connect the blue CAT5 cable:
4. Gently connect the black MicroUSB cable:
5. Check that the green, red, and two orange
lights illuminate.
Laptop Setup:
6. Open a web browser (such as Google
Chrome):
7. Type “192.168.1.1:8888” into the address
bar, and press Enter:
8. Click “New Notebook” in the top-right
corner to load up the coding section.
Radio Frequencies:
1. 87.6
2. 88.0
3. 88.4
4. 95.8
5. 96.0
6. 96.2
7. 101.3
8. 101.5
9. 101.7
10. 104.1
11. 104.5
12. 104.9

More Related Content

DOCX
C programming perso notes
PDF
Tutorial basic of c ++lesson 1 eng ver
PDF
Microcontroladores: Programación en C para microcontroladores con AVR Butterf...
PPT
Control structures pyhton
PPT
General Talk on Pointers
DOCX
PYTHON NOTES
 
PDF
Lab4 scripts
PDF
A tutorial introduction to the unix text editor ed
C programming perso notes
Tutorial basic of c ++lesson 1 eng ver
Microcontroladores: Programación en C para microcontroladores con AVR Butterf...
Control structures pyhton
General Talk on Pointers
PYTHON NOTES
 
Lab4 scripts
A tutorial introduction to the unix text editor ed

What's hot (20)

PDF
Csharp_Chap06
PPT
Java căn bản - Chapter6
PPTX
5. using variables, data, expressions and constants
PDF
Acm aleppo cpc training second session
PPTX
Python Basics
PDF
Report om 3
PPTX
Presentation 2nd
PPTX
Python Programming Homework Help
PDF
Solutions manual for absolute java 5th edition by walter savitch
PDF
PDF
Acm aleppo cpc training ninth session
PDF
Python Workshop
PPT
General Talk on Pointers
PDF
Complex C-declarations & typedef
PDF
Porting is a Delicate Matter: Checking Far Manager under Linux
PDF
Dive into exploit development
PPTX
Mastering Python lesson 4_functions_parameters_arguments
PDF
SEH based buffer overflow vulnerability exploitation
PPTX
Python basics
PDF
Acm aleppo cpc training introduction 1
Csharp_Chap06
Java căn bản - Chapter6
5. using variables, data, expressions and constants
Acm aleppo cpc training second session
Python Basics
Report om 3
Presentation 2nd
Python Programming Homework Help
Solutions manual for absolute java 5th edition by walter savitch
Acm aleppo cpc training ninth session
Python Workshop
General Talk on Pointers
Complex C-declarations & typedef
Porting is a Delicate Matter: Checking Far Manager under Linux
Dive into exploit development
Mastering Python lesson 4_functions_parameters_arguments
SEH based buffer overflow vulnerability exploitation
Python basics
Acm aleppo cpc training introduction 1
Ad

Similar to Coding in Disaster Relief - Worksheet (Advanced) (20)

PPTX
Programming with python
PDF
cos 102 - getting into programming with python.pdf
PPTX
PYTHON PROGRAMMING
PPTX
An Introduction : Python
PDF
python notes.pdf
PDF
pythonQuick.pdf
PDF
python 34💭.pdf
PPTX
Python programing
PPTX
IoT-Week1-Day1-Lab.pptx
PPTX
made it easy: python quick reference for beginners
PPTX
lecture 2.pptx
PPTX
Bikalpa_Thapa_Python_Programming_(Basics).pptx
PPTX
Python Programming Full Course || Beginner to Intermediate || Bangla (বাংলা) ...
PPTX
Keep it Stupidly Simple Introduce Python
PPTX
Teach The Nation To Code.pptx
PPTX
UNIT – 3.pptx for first year engineering
PPTX
Dr.C S Prasanth-Physics ppt.pptx computer
PPT
Computer 10 Quarter 3 Lesson .ppt
PPTX
Python Workshop - Learn Python the Hard Way
Programming with python
cos 102 - getting into programming with python.pdf
PYTHON PROGRAMMING
An Introduction : Python
python notes.pdf
pythonQuick.pdf
python 34💭.pdf
Python programing
IoT-Week1-Day1-Lab.pptx
made it easy: python quick reference for beginners
lecture 2.pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Python Programming Full Course || Beginner to Intermediate || Bangla (বাংলা) ...
Keep it Stupidly Simple Introduce Python
Teach The Nation To Code.pptx
UNIT – 3.pptx for first year engineering
Dr.C S Prasanth-Physics ppt.pptx computer
Computer 10 Quarter 3 Lesson .ppt
Python Workshop - Learn Python the Hard Way
Ad

Coding in Disaster Relief - Worksheet (Advanced)

  • 1. 1 Coding in Disaster Relief Situation Report: Introduction: Welcome to our village, engineering team. Your task is to learn the Python language and use it program a transmitter module to send a distress signal so the village can be evacuated before the hurricane arrives. Please read this worksheet carefully; you will need to use everything you will learn to save these people. Good luck. Explanation (Functions and Arguments): A function is an operation in code that performs an action if you give it an argument (or input). Let’s try using the print() function to display a message. Advanced Commenting & Printing: People write comments to explain code, if it’s not easy to understand. It doesn’t get read by the processor. In Python, comments are marked by a hash # symbol. When printing, you need to put the argument in quotation marks (“”). It is then called an input of type String. Advanced Task: Can you use the print function to evaluate mathematical expressions? Try finding the answer to 8+3.2 8 × 10 using code. (See hint) Code (Python): # Printing “Hello World” will send a # message to the console! # (print) is the function, # (“Hello World”) is the argument. print(“Please send help.”) >>> Please send help. # Only write the first line in Python; # this is called the input. # The second line starting with “>>>” # is what you should see after you run # the code. # You can press Shift-Enter to get the # computer to run the code. # More types of input data integer = 82 float = 37.462 string = ‘# of villagers, avg age.’ boolean = True array = [0, 1, 2, 1, 3, 1, 2, 1, 0] # Hint: check this out! print((0.4 + 1.8) * 2) Remember: 1. Use parentheses ( ) properly! Functions always have a pair of parentheses after them, inside which arguments go. 2. Use quotation marks “” or ‘’ properly! Strings always need to be surrounded by these when printing, but it doesn’t matter which one – so long as it’s the same. 3. Python is caSE-SenSiTiVE! Make sure you use proper casing when writing code.
  • 2. 2 Coding in Disaster Relief Coding Basics: Explanation (Variables): A variable is a name given to a stored piece of data. Variables can store numbers, strings, or Boolean (True/False). Referring to that variable will then lead you to the data stored. Explanation (Statements & Conditions): A statement is a way to make a decision. The code inside an IF block, for example, will only run if the condition is true. The condition is the comparison, or check, you use in the statement. Advanced Conditions: IF-ELIF-ELSE statements allow for much more complicated decisions to be made. Advanced Task: Can you write a series of IF-ELIF-ELSE statements that returns the name for the shape with a certain number of sides? Make sure it can handle shapes with 3 sides up to 8 sides (an octagon). Code (Python): # Variables can even store one another! volunteers = 8 students = 24 dist = students / volunteers print(dist, " students per volunteer.") >>> 3 students per volunteer. # First, we set the variable. heavy_weather_warning = True # Then, we use an if statement. if heavy_weather_warning is True: print(“Take an umbrella!”) >>> Take an umbrella! # Hint: Check this out! n_sides = 4 if n_sides == 3: print(“Triangle”) elif n_sides == 4: print(“Square”) ... >>> Square Remember: 1. Use indentation properly! Code inside IF-ELIF-ELSE statements always needs to be indented. 2. Any sort of statement needs a colon (:) after its conditions to end it. 3. Press Shift-Enter to execute (run) the code when you’re done.
  • 3. 3 Coding in Disaster Relief Coding Basics: Explanation (Loops): Sometimes you want code to repeat. Loops are a special sort of statement that can do that by setting conditions. There are two main ways to do this. The first is using a FOR loop. You get to choose the number of times the code inside the FOR loop repeats. Note that loop counts always start from 0, not 1, which is why we add the +1 in the loop condition. The other type of loop is a WHILE loop. Here, the code will keep looping until a condition is met that will cause the loop to stop. This is useful if you don’t know the exact number of times you’d like to loop the code. Advanced (Loops): Loops can be placed inside other loops. For each repetition in an outside FOR loop, all the repetitions of an inside FOR loop will be run. Advanced Task: Can you write a loop to print out all the multiplication tables from 1 to 9? Code (Python): # This is a for loop. nHomes = 30 for i in range(nHomes + 1): print(“People: ” + str(i * 5)) >>> People: 0 >>> People: 5 >>> People: 10 ... >>> People: 150 # Beware! While loops will usually # require external variables! nNeed = 150 nWater = 80 # This is a while loop. # while the number of people needing # water is greater than 0, loop code: while nNeed > 0: print(str(nNeed) + “ need water.”) if nWater > 0: # if we have water, distribute. if nNeed <= nWater: nNeed = nNeed – nWater print(str(nWater) + “ ... given.”) nWater = 0 else: nWater = nWater - nNeed print(str(nNeed) + “ ... given.”) elif nWater == 0: # if we don’t have water, call # in a supply helicopter nWater = 80 print(“Supplies requested.”) # if no-one needs water, the loop ends. print(“Everyone now has enough water!”) >>> 150 need water. >>> 80 given. >>> 70 need water. >>> Supplies requested. >>> 70 need water. >>> 80 given. >>> Everyone now has enough water! # Hint: complete the following code. # remember, you can print variables! for i in range(1, 9): for j in range(...): print(“...”) Remember: 1. Just like IF-ELIF-ELSE statements, loops require code to be indented properly! 2. When working with a WHILE loop, you need to set a condition where the loop can end, otherwise it’ll keep going forever!
  • 4. 4 3. Press Shift-Enter to execute (run) the code when you’re done. Coding in Disaster Relief Miscellaneous Code: And you’re almost done with the basics! There’s just a few final things we need to do before you can code your beacon. Explanation (Importing): Python has a number of built-in functions (print(), int(), etc) you can use to perform actions. However, sometimes the functions we want to use aren’t built-in. We will need to either define them ourselves, or import functions from other collections, called libraries. The time library gives us access to many functions to do with date and time, including sleep(), which is used for delaying code. Explanation (Delays): Code runs really fast, depending on the processor. Usually, each line of code takes less than a thousandth of a second to run! If we want to do something like flash a light every second, we need to add a delay to the code using the sleep() function. Custom Functions: What if we didn’t have a built-in function, and couldn’t find it in any library? We’d have to make our own function! Advanced Task: Can you write a function that takes a number, and returns the number of factors that number has? Code (Python): # We want to import the sleep() # function from the time library. from time import sleep # This is how a sleep timer works. # The sleep() function is part of the # time library. timer = 5 print(“Flare lit, stand back!”) while timer > 0: print(timer) sleep(1) timer = timer – 1 print(“Emergency flare fired!”) >>> Flare lit, stand back! >>> 5 >>> 4 >>> 3 >>> 2 >>> 1 >>> Emergency flare fired! # This is how functions are defined. # The first line defines the name, # and the inputs it should have. def isEven(n): if n % 2 == 0: # if the remainder of n ÷ 2 is 0 return True else: # if n ÷ 2 gives a remainder return False # Hint: complete the following code. import math def nFactors(n): factor = 0 count = 1 half = math.floor(n / 2) while count < half: ... return factorcount Remember: 1. Just like decision and loop statements, functions require code to be indented properly!
  • 5. 5 2. Be sure to include a return statement, so that the function can properly give you an answer. 3. Press Shift-Enter to execute (run) the code when you’re done. Coding in Disaster Relief
  • 6. 6 Workshop Activity: Radio Signals: You’re now ready to reprogram that transmitter module, engineering team. Remember, our goal is to broadcast a message so that a nearby radio can pick it up, and send help. Start with importing the library “ewb”. We’ll need this to be able to broadcast signals. How do we import libraries again? We’ll need to use the code on the right to start the radio broadcast. The first line starts the code that allows radio signals to be broadcast on a frequency, and the second line transmits a message on that frequency. What if the radio isn’t turned on when it’s sent? We’ll want to loop the broadcast so that it keeps repeating. What sort of loop should we use? What condition would make it loop forever? We should add a delay after the message is sent inside the loop to make it clearer for rescuers to hear us. Did we need to import a library for the delay? What function added the delay to the code? And we should be done! Run your code, and get a volunteer to check it over. Hopefully you can hear your message over the radio! Advanced Task: Can you write code to broadcast the date and time as well as the location in the message? Code: # Replace the ‘...’ with your frequency radio = ewb.Radio(...) # Replace the ‘...’ with a message radio.say(“...”) # You might need these # following snippets of code: import datetime current = time.strftime(“%c”) Remember: Check the previous pages of the worksheet if you need any help!
  • 7. 7 Raspberry Pi Setup: 1. Disconnect the laptop from the internet: 2. Insert the SD Card into the Raspberry Pi: 3. Gently connect the blue CAT5 cable: 4. Gently connect the black MicroUSB cable: 5. Check that the green, red, and two orange lights illuminate. Laptop Setup: 6. Open a web browser (such as Google Chrome): 7. Type “192.168.1.1:8888” into the address bar, and press Enter: 8. Click “New Notebook” in the top-right corner to load up the coding section. Radio Frequencies: 1. 87.6 2. 88.0 3. 88.4 4. 95.8 5. 96.0 6. 96.2 7. 101.3 8. 101.5 9. 101.7 10. 104.1 11. 104.5 12. 104.9