Grade+2+Unit+4+ +Python+Programming+
Grade+2+Unit+4+ +Python+Programming+
Grade 2
Unit 4
Python Programming
www.theacademyofcode.com/handouts
Lesson 1 - Input/Output Devices
Learning Outcomes:
● Treating computers with care
● Input and Output devices
● Design a poster
● Coding Basics - Lightbot
Some devices input data into the computer. Some devices output data.
Have a look at the tech items below. In which categories do they belong?
2
Talking Time
Why?
How many input and output devices can you see in this room?
3
Game Time - Typing Skills
We’ve had an awesome 6 weeks with HTML and CSS. We’re about to
start into a module on Python which will involve a bit of typing. Lets polish up on
our typing skills. Today’s three typing activities are:
● BBC Dancemat
○ https://www.taoc.ie/bbcdm1
○ https://www.taoc.ie/bbcdm2
○ https://www.taoc.ie/bbcdm3
● Keyboard Climber
○ https://www.taoc.ie/keyboardclimber
● Typing Attack
○ https://taoc.ie/typeattack
For the remainder of the class, we’re going to use python to battle
through the dungeon in CodeCombat. This is a fun way to practice and learn the
python programming language to a high high level.
Block Turtle
Tina the turtle moves around your screen just like the mouse arrow,
but unlike the mouse, Tina leaves a trail behind her. Tina can draw all kinds
of shapes and pictures - you just need to give them the right commands.
If there are blocks already here, Right Click on this white area and
Left Click on ‘Delete Blocks’ to clean up your workspace.
Now we’re ready to code. We are going to draw a simple shape (a circle)
using Tina the Turtle.
Set the colour that the turtle will use to draw the shape.
Challenge
If you’ve finished this tasks, play around with the other blocks for a few
minutes to see what else you can do with Tina.
See if you can:
● Make Tina draw another circle
○ in a different colour
○ In a different position
● Make Tina write some text
● Try and position a few circles in different areas (we’re doing this
next week, so don’t worry if you can’t).
7
Lesson 3 - Block Turtle II
Learning Outcomes:
● Working out different locations for Tina to get to on a map.
● Drawing multiple circles and different x & y coordinates
Game Time
For the first few minutes, have a go at whatever games you didn't try in last
week's warm up.
● BBC Dancemat
○ https://www.taoc.ie/bbcdm1
○ https://www.taoc.ie/bbcdm2
○ https://www.taoc.ie/bbcdm3
● Keyboard Climber
○ https://www.taoc.ie/keyboardclimber
● Typing Attack
○ https://taoc.ie/typeattack
Whale Lighthouse
(100,_____) (-100,_____)
Cannonballs Bomb
(____,____) (____,____)
Treasure Anchor
(____, 150) (____,____)
9
Task 1 - Drawing Two Circles
www.bit.ly/blankblocktrinket
Put the pen back down on the screen using Pen Down
so that Tina can start drawing again.
Challenge
Try making the image these images.
11
Now it’s time to Save our Work
Click Link
Extra Challenge
If you’re teacher is happy with your work and when you’ve saved it
properly, play the Basics section (Part 1) of Lightbot at this LINK.
12
Lesson 4 - Block Turtle III
Learning Outcomes:
● Making Tina draw some simple shapes at different
locations on the map - Olympics Rings
● Making a miniature house
● Make other shapes with Tina
Game Time
Today we’re going to look at how to draw different shapes apart from just
circles. Before we look at how to draw these shapes we’re
going to play Lightbot which can be accessed at
www.lightbot.com/flash.html. The concepts we will learn in
Lightbot will link in nicely with drawing some of these shapes.
Use what you have learned in this lesson to write code to make Tina the Turtle draw
the Olympic flag. https://taoc.ie/olympicstarter
Drawing Squares
We can draw shapes other than circles. Although drawing other shapes
isn’t just as easy. Think about how you
would make Lightbot move to make
a square.
14
Expert Tip
Now Tell Tina to do the following code
Be careful where you
put the fill or color
commands. You will get
different results depending
on where you put them.
Other Shapes
Expert Tip
The angle which Tina turns
isn’t the degrees of each angle.
The Turn Angle or Outside
Angle is calculated by taking the
inside angle away from 180°.
eg Pentagon is 180° - 108° = 60°
Hexagon is 180° - 60° = 120°
15
Lesson 5 - From Blocks to Code I
Learning Outcomes:
● Learning how to make text with Tina the Turtle
● Make a pizza for Fat Tony’s Pizza company
● Converting block python into typed python code (Grade 2)
Hint: Once you have drawn the outer crust and base of the
pizza, draw different shapes with different colours to draw
your different toppings.
Finished Early?
Try recreating some of the advanced art cards at:
https://wiki.sugarlabs.org/go/Activities/Turtle_Art/Turtle_Cards
17
Lesson 6 - From Blocks to Code II
Learning Outcomes:
● Revising Shapes and introducing For Loops
● Make a house scene with as many items as possible with
the view to using different shapes
Click on one of the links below and for practice draw and fill
each one of the shapes on the right.
Block Version:
https://taoc.ie/turtle4
Code Version:
https://taoc.ie/replturtle4
18
House Scene Challenge
Block Version: https://trinket.io/embed/blocks/563fe95f6b
Code Version: https://repl.it/@DamienAOC/Python-House
Stop Sign
Extra Challenge
Try recreating some of the advanced art cards at:
https://wiki.sugarlabs.org/go/Activities/Turtle_Art/Turtle_Cards 19
Traffic Scene (to be worked on over two lessons/ for homework)
Block Version: https://taoc.ie/trafficstarter (Only use if you really need)
Code Version: https://taoc.ie/repltrafficstarter
Start by trying to draw a car or maybe a skyscraper. Then after that, it’s up to you!
NB: If you intend to do a big scene or even if you just feel like being lazy and calling upon
your two best mates ‘Copy’ and ‘Paste’ to save you time (find me someone who doesn’t?!)
PLEASE PLEASE PLEASE USE THE CODE VERSION instead of the block version.
It will save you so much time and hassle and you’ll be coding like a pro!
20
YOU CAN DO THIS!!
Lesson 7 - From Blocks to Code III
Learning Outcomes:
● To Learn the importance of For Loops and Variables
● Making a Shape Maker App
● Letting Python Do the Maths
Bad Code Better Code (Less work!) The ‘Real’ python code
Students who have used Scratch before will know variables as being a
container of data, data which can change in a programme - score, bullets left,
X-position, the amount of coins collected etc. If we wanted the above shape to
change when the user wants it to, we could use variables.
Variables
Incase you haven’t notices, ALL codes on this page do this exact same thing, but
some are more professional than others.
21
Making a Shape Maker App
Starting code: https://taoc.ie/shapemakerstarter
Your task is to turn the starting code into the code below. The app has 5 variables,
sides, length, colour, speed (values all determined by the user) as well as angles
(which the computer calculates itself by dividing 360 by ‘sides)’.
Extra Challenge
Develop the code on the right
to make a Custom Shape Spirograph
Making app. The variable
will need to be another question.
NB Watch out for those tabbed spaces in the for loop! 22
Letting Python do the Maths
People who don’t know much about code often think that you need to be good
at maths to be good at coding. However, smart coders get computers to do the
maths for them! We’re going to let Python work out how old a user will be in 2100.
CTRL+CLICK https://taoc.ie/blankpython
and type in the code as it appears here.
Instead of 2009, put in your birth year).
Run your program and then enter the year you were born. Did you get another
error message? That’s because anything typed into your program is text, and it
needs to be converted to a
number. You can use int() to
convert the text to an integer
(‘integer’ means ‘whole number’ like 5, 929 or 1,000,000 not 2.1 or 5.65 etc).
23
Extra Challenge - Dog Years
Your challenge is to write a programme that asks users their age and then tells
them their age in dog years.
Expert Tip
In python, the symbol
for multiplication is *, and
is usually shift+8 on the
keyboard.
24
Lesson 8 - Python II - Rock Paper Scissors
Learning Outcomes:
● Creating a rock/paper/ scissors game
● Using simple if statements
● Announcing the winner
First, we’re going to let the player choose Rock, Paper or Scissors by typing
r, p or s. Add the blue lines of code. The second will print the player’s choice.
Now to code the computers guess. randit will generate a random number to
decide between rock,
paper and scissors.
Add in the blue lines
and test it a few times
to see that you’re getting a random number.
25
Coding the Computer
In our code, 1 = rock (r), 2 = paper (p) and 3 = scissors(s). We’re going to
use an if statement to check if the number 1 (for example) is randomly chosen, the
computer will see that as a rock. Add the lines in blue making sure to indent (move
to the right). We can use two spaces or a ‘tab’ for this. Tab is usually above the
Caps Lock button on your keyboard.
The computer’s choice gets printed on a new line. You can fix that by adding
end=' ' after vs. This tells Python to end with a space instead of a new line. Play
the game a few times by clicking Run and making a choice. For now you’ll have to
work out who won yourself. Next you’ll add the Python code to work this out.
26
Announcing the Winner
Lets look at a case where the player choses ‘r’ (rock). If the computer chose ‘s’
(scissors) then the player wins (rock beats scissors). If the computer chose ‘p’
(paper) then the computer wins (paper beats rock). We can check the player choice
and the computer choice using the word and.
Add in more elif’s so that every ‘p’ and ‘s’ combination is covered.
Can you create your own game like Rock, Paper, Scissors with different objects?
Click the ‘Duplicate’ button to make a copy of your Rock, Paper Scissors project to
start from.
27
Lesson 9 - Python III - Turtle Race
Learning Outcomes:
● Make a random race which races turtles for fun
● Learning about for loops
● Customising your game
28
For Loops - Making it easy
To draw the lines, we’re going to have to turn the turtle to the right, before
putting a pen down, drawing a line of 150, picking the penup and moving backward to
the start of the line. Add the code in blue below and see what happens.
right(90) makes the turtle turn right 90 degrees (a right angle.) Moving
forward(10) before putting the pen down leaves a small gap between the number
and the start of the line. After drawing the line you lift up the pen and go
backward(160) the length of the line plus the gap.
29
Racing Turtles
The first line creates a turtle called ‘red’. The next lines set the colour and
shape of the turtle. Now it really looks like a turtle!
Now you need to make the turtle race by moving a random number of steps
at a time. You’ll need the randint
function from the Python random
library. Add this import line to the
top of your script. The randint function returns a random number between the
values chosen. The turtle will
move forward 1, 2, 3, 4, or 5
steps each turn.
Test the code and you should get a one-turtle race! We’ll add more next.
30
Adding more Turtles
Copy
Paste going.
31
Challenge: Do a twirl
Can you use a for turn in range(): loop to make each turtle do a 360 degree
twirl after they get to the starting line? You’ll need to make sure they are facing in the
right direction at the start of the race!
Hint: A full turn is 360 degrees. A turtle could turn right 10 degrees 36 times, or left 5
degrees 72 times, or any other numbers make 360!
Can you use a loop to make the track lines dashed instead of solid?
32
Lesson 10 - Python Project IV - ISS
Learning Outcomes:
● Using Web Service to find the location of the International
Space Station
● Plotting its location on a map.
Introduction
In this project you will use a web service to find out the current location of the
International Space Station (ISS) and plot its location on a map.
Open http://api.open-notify.org/astros.json
in a web browser (Chrome, Internet Explorer,
Firefox etc.). You should see something like
what is here (the data is live so your figures will
be different). The data format is called JSON
(pronounced Jay-Son). 33
Let’s call the web service from Python so we can use the results. Open this
project file: www.taoc.ie/issrepl (repl) or www.taoc.ie/iss (trinket). The
urllib.request and json modules have already been imported for you.
Add the following code to main.py to put the web address you just used into a
variable:
34
Now you need to print out a line for each astronaut.
different astronaut.
You can then look up the values for ‘name’ and ‘craft’
People in Space:
Yuri Malenchenko
Timothy Kopra
Timothy Peake
You are using live data so your results will depend on the number of people currently
in space.
As well as the name of the astronaut the web service also provides the craft that
they are in (such as the ISS.)
Can you add to your script so that it also prints out the craft that the astronaut is in.
Example:
People in Space: 3
Yuri Malenchenko in ISS
Timothy Kopra in ISS
Tim Peake in ISS
Step 2: Where is the ISS?
The International Space Station is in orbit around Earth. It orbits the earth roughly
every hour and a half. The ISS travels at an average speed of 7.66 km per second.
Let’s use another web service to find out where the International Space Station is.
First open the url for the web service in a new tab in your web browser:
http://api.open-notify.org/iss-now.json
You should see something like this:
Refresh the page a few times to see the position constantly being updated. The
result contains the coordinates of the spot on Earth that the ISS is currently over.
Longitude is the East-West position and runs from -180 to 180. 0 is the Prime
Meridian which runs through Greenwich in London, UK.
Latitude is the North-South position and runs from 90 to -90. 0 is the Equator.
Now you need to call the same web service from Python. Add the following code to
the end of your script to get the current location of the ISS and make variables to
store and display its positions.
Let’s load a world map as the background image, there’s one already included
in your trinket.
NASA have provided this map and given permission for reuse. The map is centered
at 0, 0 which is just what you need. You need to set the screen size to match the
size of the image which is 720 by 360.
Note that latitude is normally given first, but we need to give longitude first when
plotting (x,y) coordinates.
Test your program by running it. The ISS should move to its current location above
Earth. Wait a few seconds and run your program again to see where the ISS has
moved to.
[the academy_of_code] 2019
Icons used with thanks to Freepik