Connect to UWA WiFI
1. Select UnifiGuest wireless network
�2. Enter the username and password
Username: perthgpn
Password: 8Sq jRd8?
�3. Accept the Terms of Use.�4. Click the "Log in" button.�5. You're connected!
Welcome to the Labs
Welcome to the labs!
Tamagotchi! - Micro:bits
Thank you to our Sponsors!
Platinum Sponsor:
Gold Sponsor:
Who are the tutors?
Who are you?
Things in Common
Log on
Tell us you’re here!
Click on the
Start of Day Survey
and fill it in now!
Come to the front to get your ticket for the free prize draw when you’ve finished!
Log on
Click on your Room picture
You can see:
Using the workbook!
The workbooks will help you put your project together!
Each Part of the workbook is made of tasks!
Task 6.1: Make the thing do blah! |
Make your project do blah …. |
Hint |
A clue, an example or some extra information to help you figure out the answer. |
Task 6.2: Add a blah to your code! |
This has instructions on how to do a part of the project
|
Tasks - The parts of your project
Follow the tasks in order to make the project!
Hints - Helpers for your tasks!
Stuck on a task, we might have given you a hint to help you figure it out!��The hints have unrelated examples, or tips. Don’t copy and paste in the code, you’ll end up with something CRAZY!
print('This example is not part of the project’) |
Using the workbook!
The workbooks will help you put your project together!
Check off before you move on from a Part! Do some bonuses while you wait!
★ BONUS 4.3: Do something extra! |
Something to try if you have spare time before the next lecture! |
Checklist - Am I done yet?
Make sure you can tick off every box in this section before you go to the next Part.
Lecture Markers
This tells you you’ll find out how to do things for this section during the names lecture.
CHECKPOINT |
If you can tick all of these off you’re ready to move the next part! ☐ Your program does blah ☐ Your program does blob |
Bonus Activities
Stuck waiting at a lecture marker?
Try a purple bonus. They add extra functionality to your project along the way.
Today’s project!
Tamagotchi - Micro:Bit
Tamagotchi
Tamagotchi
Sadly you can’t keep them at the end of the day. 😥
If you want one for home (maybe for christmas or your birthday!) they’re about $25 .
Find out where to buy them here:
Intro to Micro:Bit
What is a Micro:Bit?
Buttons: We can press these and tell the Micro:Bit to do different things
Lights: We can turn each light on or off to make different images
Pins: These let us connect the Micro:Bit to other devices using wires
Button A
Button B
Lights!
Pins
Front
What is a Micro:Bit?
Reset button
Input
Input: Where we connect the cable from the computer to transfer our code/power to our Micro:Bit
Reset button: Let’s you stop your code and starts it again
Battery connection: You can use your micro:bit even when it is not plugged into your computer! Ask you tutor for a battery pack if you need one.
Accelerometer: The Micro:bit can tell us when it is accelerated - so it knows when we shake it!
Back
Battery connection
Accelerometer
Using python.microbit.org
Go to python.microbit.org
Today we will be using python.microbit.org to program our Micro:Bits.
You should see this page pop up!
python.microbit.org
This is where we code
This is the simulator where we test our code
How do we write code for it?
Micro:Bits use Python, which is the programming language that we usually teach here at GPN!
Always make sure this line is at the top of your code!
This lets us use lights, sounds, buttons and lots of other cool in our Python code for the Micro:Bit
from microbit import *
The Display
Your Micro:Bit has a 5 x 5 display grid of little red LEDs on the front!
You can do some cool stuff with the display like:
Show an image, like a heart!
Scroll a word across the display, like ‘Hello’
This code is in your python.microbit.org coding space - have a look
It’s indented in a while loop - so it will repeat forever
To show or to scroll….
Remember our microbit has a 5x5 grid of lights.
display.scroll('Hi!')
display.show(Image.SAD)
So if you want to display long words or messages, use scroll. If you want to display something that fits in a 5x5 grid, use show!
Using the Simulator
Restart
Stop
We can run our code on the Simulator or the real micro:bit!
Stop, Restart, Simulator settings are underneath
Connect the Micro:Bit
Run the code on the Micro:Bit (Chrome/Edge)
It’s fun to mess around with the Micro:Bit on the simulator.
Now let’s see your code on a Micro:Bit in real life!
Run your code on your Micro:Bit like this
You should see a HEART displayed for 1 second and then HELLO
Want your code to start again? Press black “reset” button on the back
Chrome or Edge
Run the code on the Micro:Bit (other browser)
This is for if you don’t have the Chrome or Edge browser (eg Safari)
Run your code on your Micro:Bit like this
You should see a HEART displayed for 1 second and then HELLO
Want your code to start again? Press black “reset” button on the back
Mistakes are Great! Errors on the Micro:bit!
I ❤️ errors!
We can learn from our mistakes!
2. What went wrong
1. Where the error is
Project Time!
Let’s use our MicroBit!
Try Parts 0 & 1 of your Workbook!�� The tutors will be around to help!
Variables
No Storing is Boring!
It’s useful to be able to remember things for later!
Computers remember things in "variables"
Variables are like putting things into a labelled cardboard box.
Let’s make our favourite number 8!
In our code we make a variable and set it to a value like this:
fav_num = 8
fav_num
8
Variables
Variables are useful for storing things that change
Variables contain data that "vary" - hence the word "variable".
Let’s change fav_num to 102.
fav_num = 102
fav_num
102
Reusing variables
We can replace values in variables and show it:
animal = "dog"
display.scroll(animal)
animal = "cat"
display.scroll(animal)
animal = animal + "dog"
display.scroll(animal)
What will this scroll?
Reusing variables
We can replace values in variables and show it:
animal = "dog"
display.scroll(animal)
animal = "cat"
display.scroll(animal)
animal = animal + "dog"
display.scroll(animal)
What will this scroll?
Reusing variables
We can replace values in variables and show it:
animal = "dog"
display.scroll(animal)
animal = "cat"
display.scroll(animal)
animal = animal + "dog"
display.scroll(animal)
What will this scroll?
Reusing variables
We can replace values in variables and show it:
animal = "dog"
display.scroll(animal)
animal = "cat"
display.scroll(animal)
animal = animal + "dog"
display.scroll(animal)
What will this scroll?
While Loops
Loops
We know how to do things on repeat!
Sometimes we want to do some code on repeat!
Introducing … while loops!
What do you think this does?
i = 0
while i < 3:
display.scroll(i)
i = i + 1
Introducing … while loops!
What do you think this does?
i = 0
while i < 3:
display.scroll(i)
i = i + 1
Introducing … while loops!
Stepping through a while loop...
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
MY VARIABLES
Set the variable
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
MY VARIABLES
0 is less than 3 !
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
MY VARIABLES
Print !
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
i = 1
MY VARIABLES
UPDATE TIME !
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
i = 1
MY VARIABLES
Take it from the top !
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
i = 1
MY VARIABLES
1 is less than 3 !
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
i = 1
MY VARIABLES
Print !
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
i = 1
i = 2
MY VARIABLES
UPDATE TIME !
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
i = 1
i = 2
MY VARIABLES
Take it from the top !
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
i = 1
i = 2
MY VARIABLES
2 is less than 3 !
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
i = 1
i = 2
MY VARIABLES
Print !
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
i = 1
i = 2
i = 3
MY VARIABLES
UPDATE TIME !
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
i = 1
i = 2
i = 3
MY VARIABLES
Take it from the top !
Introducing … while loops!
One step at a time!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
i = 0
i = 1
i = 2
i = 3
MY VARIABLES
3 IS NOT less than 3 !
We are are done with this loop!
Introducing … while loops!
i = 0
while i < 3:
display.scroll(i)
i = i + 1
Initialise the loop variable
Loop condition
Code to repeat
Update the loop variable
What happens when…..
What happens if we forget to update the loop variable?
i = 0
while i < 3:
display.scroll(i)
What happens when…..
What happens if we forget to update the loop variable?
i = 0
while i < 3:
display.scroll(i)
Infinite loop!
Sometimes we want our loop to go forever!
So we set a condition that is always True!
We can even just write True!
while True:
display.scroll("Are we there yet?")
Infinite loop!
Sometimes we want our loop to go forever!
So we set a condition that is always True!
We can even just write True!
while True:
print("Are we there yet?")
while True:
display.scroll("Are we there yet?")
Are we there yet? Are we there yet?
Infinite loops and Micro:Bits
Your Micro:Bit runs code really fast!
When we want to do things like button presses, we need it to check for a button press over and over and over again so that it’s just always checking!
This means we want to put almost all of our code inside the while True loop!
The only code we want to have before the while loop is stuff we only want to happen once like setting variables or saying hello!
Indentation
Whenever we have an if statement or while loop, there is something we have to do to make sure it only runs what we want it to run inside the if statement.
… that is called indentation
while True:
if num>10:
display.scroll('a big number')
These gaps are indentation!
Indentation
Whenever we have an if statement or while loop, there is something we have to do to make sure it only runs what we want it to run inside the if statement.
… that is called indentation
We use the indentation to tell the code that a piece of code is "inside" another, for loops this means any code that has at least one extra gap after the loop, will be run.
while True:
if num>10:
display.scroll('a big number')
But how do we indent?
There a couple ways to make sure a line of code is indented.
One is pressing the TAB button on your keyboard before a line of code.
Another is selecting the lines you want to indent and pressing the TAB button to indent them all at once.
And the last main one is to select all the lines you want to indent and press the CTRL and the ] button at the same time.
Remember you need to indent for your code to work right!
Micro:Bit Instructions
Scroll… Scroll… Scroll… on the micro:bit
Words are too big to display within a 5x5 grid of lights.
Remember we can display words with display.scroll().
Sometimes the text scrolls across too slowly - you can speed it up with delay.
A smaller delay (eg 100 results in faster scrolling).
The default speed is 150!
display.scroll('Hello World')
display.scroll('Hello World', delay=100)
Multiple Instructions
What happens if we want to change the speed AND join variables with strings?��This is how you would do it! :)
Without a sleep, the computer will run through the code so quickly, and we will only see a CONFUSED face.
See that we need to use str( ) to convert the number win_count to a string before we can join it (+) with the the other string!
win_count = 3
display.scroll('Wins: '+ str(win_count), delay=75)
Message
delay
Sleep… zzz! … on the micro:bit
Computers are really fast, sometimes our program moves too quickly to enjoy it!
For example:
We can slow it down by using sleep()
Sleep is done in milliseconds (so the number of seconds x 1000)
display.show(Image.HAPPY)
sleep(1000)
display.show(Image.SAD)
sleep(1000)
display.show(Image.CONFUSED)
sleep(1000)
Without a sleep, the computer will run through the code so quickly, and we will only see a CONFUSED face.
Micro:Bit Inputs
Buttons!
Your Micro:Bit has 2 buttons: Button A and Button B
You can use this code to check if a button is pressed:
The statement will be TRUE if the button is being pressed at that time and it will be FALSE if it is not being pressed
if button_a.was_pressed():
If button_b.was_pressed():
Buttons!
What do you think this code does?
If button a is pressed when the Micro:Bit gets to this line of code then what happens?
If button b is pressed when the Micro:Bit gets to this line of code then what happens
What do you think happens if both button a AND button b are being pressed?
The Micro:Bit shows a Sad face
if button_a.was_pressed():
display.show(Image.HAPPY)
if button_b.was_pressed():
display.show(Image.SAD)
Buttons!
What do you think this code does?
If button a is pressed when the Micro:Bit gets to this line of code then what happens?
If button b is pressed when the Micro:Bit gets to this line of code then what happens
What do you think happens if both button a AND button b are being pressed?
The Micro:Bit shows a Sad face
if button_a.was_pressed():
display.show(Image.HAPPY)
if button_b.was_pressed():
display.show(Image.SAD)
The Micro:Bit shows a Happy face
Buttons!
What do you think this code does?
If button a is pressed when the Micro:Bit gets to this line of code then what happens?
If button b is pressed when the Micro:Bit gets to this line of code then what happens
What do you think happens if both button a AND button b are being pressed?
The Micro:Bit shows a Sad face
if button_a.was_pressed():
display.show(Image.HAPPY)
if button_b.was_pressed():
display.show(Image.SAD)
The Micro:Bit shows a Happy face
Buttons!
What do you think this code does?
If button a is pressed when the Micro:Bit gets to this line of code then what happens?
If button b is pressed when the Micro:Bit gets to this line of code then what happens
What do you think happens if both button a AND button b are being pressed?
The Micro:Bit shows a Sad face
It will show one face and then the other immediately
if button_a.was_pressed():
display.show(Image.HAPPY)
if button_b.was_pressed():
display.show(Image.SAD)
The Micro:Bit shows a Happy face
Pin Logo!
Your Micro:Bit has touch sensitive pin logo at the top of the Micro:bit.�
You can use this code to check if the pin logo is being touched.
Pin Logo
if pin_logo.is_touched():
Running Time
Sometimes you want to time things. Like, for example, if you wanted to put a time limit on a game and see how many points you can get in 30 seconds!
To figure out how long the Micro:Bit program has been running (in milliseconds) you can use this command:
What would running_time() be after 4 seconds?
What about after 10 and a half seconds?
4000
10,500
time = running_time()
Running Time
Sometimes you want to time things. Like, for example, if you wanted to put a time limit on a game and see how many points you can get in 30 seconds!
To figure out how long the Micro:Bit program has been running (in milliseconds) you can use this command:
What would running_time() be after 4 seconds?
What about after 10 and a half seconds?
4000
10,500
time = running_time()
Running Time
Sometimes you want to time things. Like, for example, if you wanted to put a time limit on a game and see how many points you can get in 30 seconds!
To figure out how long the Micro:Bit program has been running (in milliseconds) you can use this command:
What would running_time() be after 4 seconds?
What about after 10 and a half seconds?
4000
10,500
time = running_time()
Accelerometer!
Your micro:bit has a motion sensor.
This sensor has the ability to detect when you tilt it left to right, backwards and forwards and up and down.
To use the accelerometer, we need a while loop. You can use this code to detect when the micro:bit has been shaken:
while True:
if accelerometer.was_gesture('shake'):
Information from the sensor
Accelerometer!
What do you think this code does?
while True:
if accelerometer.was_gesture('shake'):
display.scroll('I’m getting dizzy')
Accelerometer!
What do you think this code does?
It will display ‘I’m getting dizzy’ every time the micro:bit is shaken
while True:
if accelerometer.was_gesture('shake'):
display.scroll('I’m getting dizzy')
Project Time!
Let’s use our MicroBit!
Try Parts 2 & 3 of your Workbook!�� The tutors will be around to help!
Functions!
Simpler, less repetition, easier to read code!
How functions fit together!
Functions are like factories!
�Running a factory doesn’t mean doing all the work yourself, you can get other factories to help you out!
Your main factory!
Timber Mill
Metal Worker
Cupcake factory
How functions fit together!
Functions are like factories!
Asking other factories to do some work for you makes your main task simper. You can focus on the assembly!
Your main factory!
I’d like to place an order for a piece of wood. 2 meters by 1.5 meters.
Sure thing! Coming right away!
Order
Delivery
Order
Delivery
Can I order 4 metal poles please! 80cm long.
Timber Mill
Cupcake factory
Metal Worker
It will be delivered straight away!
How functions fit together!
Functions are like factories!
Your main factory!
Timber Mill
Metal Worker
Cupcake factory
Look at this beautiful table I made!
Outsourcing made it simple!
How functions fit together!
You can write a bunch of helpful functions to simplify your main goal!
You can write these once and then use them lots of times!�They can be anything you like!�
Your main code!
Helps with printing nicely
Does calculations
Uses stats to make decisions
Don’t reinvent the wheel
We’re already familiar with some python in built functions like print and input!
There’s lots of functions python gives us to save us reinventing the wheel!
For instance we can use len to get the length of a string, rather than having to write code to count every letter!
>>> len("Hello world")
11
Don’t reinvent the wheel
We’re already familiar with some python in built functions like print and input!
There’s lots of functions python gives us to save us reinventing the wheel!
For instance we can use len to get the length of a string, rather than having to write code to count every letter!
>>> len("Hello world")
11
Don’t reinvent the wheel
We’re already familiar with some python in built functions like print and input!
There’s lots of functions python gives us to save us reinventing the wheel!
For instance we can use len to get the length of a string, rather than having to write code to count every letter!
>>> len("Hello world")
11
What do these do?:
>>> name = "Renee"
>>> len(name)
5
>>> int("6")
6
>>> str(6)
"6"
Don’t reinvent the wheel
We’re already familiar with some python in built functions like print and input!
There’s lots of functions python gives us to save us reinventing the wheel!
For instance we can use len to get the length of a string, rather than having to write code to count every letter!
>>> len("Hello world")
11
What do these do?:
>>> name = "Renee"
>>> len(name)
5
>>> int("6")
6
>>> str(6)
"6"
Don’t reinvent the wheel
We’re already familiar with some python in built functions like print and input!
There’s lots of functions python gives us to save us reinventing the wheel!
For instance we can use len to get the length of a string, rather than having to write code to count every letter!
>>> len("Hello world")
11
What do these do?:
>>> name = "Renee"
>>> len(name)
5
>>> int("6")
6
>>> str(6)
"6"
Don’t reinvent the wheel
We’re already familiar with some python in built functions like print and input!
There’s lots of functions python gives us to save us reinventing the wheel!
For instance we can use len to get the length of a string, rather than having to write code to count every letter!
>>> len("Hello world")
11
What do these do?:
>>> name = "Renee"
>>> len(name)
5
>>> int("6")
6
>>> str(6)
"6"
Defining your own functions
Built in functions are great! But sometimes we want custom functions!
Defining our own functions means:�
Defining your own functions
Then you can use your function by calling it!
Which will do this!
def say_hello():
display.scroll("Hi")
display.show(Image.HAPPY)
�����
say_hello()
say_hello()
Defining your own functions
Then you can use your function by calling it!
Which will do this!
def say_hello():
display.scroll("Hi")
display.show(Image.HAPPY)
�����
say_hello()
say_hello()
When using a function in a script make sure you define the function first.
It doesn’t matter if you call it from inside another function though!
Functions often need extra information
Functions are more useful if we can change what they do
We can do this by giving them arguments (aka parameters)
def hello(person):
display.scroll('Hi ' + person)
hello('Alex')
Functions often need extra information
Functions are more useful if we can change what they do
We can do this by giving them arguments (aka parameters)
Here, we give the hello() function a name
Any string will work
def hello(person):
display.scroll('Hi ' + person)
hello('Alex')
Functions often need extra information
Functions are more useful if we can change what they do
We can do this by giving them arguments (aka parameters)
def hello(person):
display.scroll('Hi ' + person)
hello('abcd')
Functions can take multiple arguments
Often we want to work with multiple pieces of information.
You can actually have as many parameters as you like!
This function takes two numbers, adds them together and prints the result.
def add(x, y):
display.scroll(x + y)
add(3, 4)
Arguments stay inside the function
The arguments are not able to be accessed outside of the function declaration.
def hello(person):
display.scroll('Hello, ' + person + '!')
display.scroll(person)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'person' is not defined
Variables stay inside the function
Neither are variables made inside the function. They are local variables.
def add(x, y):
z = x + y
display.scroll(z)
add(3, 4)
display.scroll(z)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'z' is not defined
Global variables are not affected
Changing a variable in a function only changes it inside the function.
z = 1
�def add(x, y):
z = x + y
display.scroll(z)
add(3, 4)
Global variables are not affected
Changing a variable in a function only changes it inside the function.
What's the value of z now?
z = 1
�def add(x, y):
z = x + y
display.scroll(z)
add(3, 4)
display.scroll(z)
Recap: A function signature
def add(x, y):���add(2, 3)
the def keyword
function name
function arguments
function name
function arguments
definition
callsite
Return Keyword
Remember functions are like a factory
Input
Output
Function
But when we want our output to be sent to another factory we need to use the keyword: return
Return Keyword
Here’s an example
What's displayed?
def get_name():
return “Alex”
def hello()
name = get_name()
display.scroll(“Hi ” +name)
Return Keyword
Here’s an example
What's displayed?
def get_name():
return “Alex”
def hello()
name = get_name()
display.scroll(“Hi ” +name)
Classes
What is an object?
What do you think an object is?
What is an object?
What do you think an object is?
What is an object?
What do you think an object is?
What is an object?
What do you think an object is?
What is an object?
What do you think an object is?
What is an object?
What do you think an object is?
What is an object in code?
An object is something that we know information about and that can sometimes do things
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What information might we know about a cat?
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What information might we know about a cat?
Name
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What information might we know about a cat?
Name
Age
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What information might we know about a cat?
Name
Age
Colour
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What information might we know about a cat?
Name Owner
Age
Colour
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What information might we know about a cat?
Name Owner
Age Weight
Colour
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What information might we know about a cat?
Name Owner
Age Weight
Colour Microchip #
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What things might a cat do?
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What things might a cat do?
Meow
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What things might a cat do?
Meow
Eat
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What things might a cat do?
Meow
Eat
Scratch
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What things might a cat do?
Meow Sleep
Eat
Scratch
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What things might a cat do?
Meow Sleep
Eat Purr
Scratch
What is an object in code?
An object is something that we know information about and that can sometimes do things
Like a cat!
What things might a cat do?
Meow Sleep
Eat Purr
Scratch Jump
What does that look like in Python?
Let’s have a look at how we might make a Cat object in Python code!
What does that look like in Python?
Let’s have a look at how we might make a Cat object in Python code!
class Cat():
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
Here we tell python that we are making a new type (or class) of object called Cat
What does that look like in Python?
Let’s have a look at how we might make a Cat object in Python code!
class Cat():
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
__init__ is how we tell Python how to make a new Cat
What does that look like in Python?
Let’s have a look at how we might make a Cat object in Python code!
class Cat():
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
Here we tell Python what information we need to know about the Cat
Note: self is special and we always need it
What does that look like in Python?
Let’s have a look at how we might make a Cat object in Python code!
class Cat():
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
Here we save the information we got so we can use it again
What does that look like in Python?
How do we make a new Cat?
class Cat():
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
emmy = Cat(“Emmy”, 3, “Dark brown”)
What does that look like in Python?
What does this show on the screen?
class Cat():
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
emmy = Cat(“Emmy”, 3, “Dark brown”)
display.scroll(emmy.name)
display.scroll(emmy.age)
display.scroll(emmy.colour)
What does that look like in Python?
What does this show on the screen?
class Cat():
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
emmy = Cat(“Emmy”, 3, “Dark brown”)
display.scroll(emmy.name)
display.scroll(emmy.age)
display.scroll(emmy.colour)
Emmy
3
Dark Brown
What about doing things?
We said an object was something with information that could sometimes do things. Our Cat object doesn’t do anything right now - let’s add a way for it to meow!
What about doing things?
We said an object was something with information that could sometimes do things. Our Cat object doesn’t do anything right now - let’s add a way for it to meow!
class Cat():
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
def meow(self):
display.scroll(“Meow”)
What about doing things?
What does this code do?
class Cat():
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
def meow(self):
display.scroll(“Meow”)
emmy = Cat(“Emmy”, 3, “Dark brown”)
emmy.meow()
What about doing things?
What does this code do?
class Cat():
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
def meow(self):
display.scroll(“Meow”)
emmy = Cat(“Emmy”, 3, “Dark brown”)
emmy.meow()
Meow
What else can it do?
Let’s have our cat have a Birthday that makes it get older by 1 year!
What else can it do?
Let’s have our cat have a Birthday that makes it get older by 1 year!
class Cat():
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
def meow(self):
display.scroll(“Meow”)
def birthday(self):
self.age = self.age + 1
What else can it do?
What does this code do?
class Cat():
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
def meow(self):
display.scroll(“Meow”)
def birthday(self):
self.age = self.age + 1
emmy = Cat(“Emmy”, 3, “Dark brown”)
emmy.birthday()
display.scroll(emmy.age)
What else can it do?
What does this code do?
class Cat():
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
def meow(self):
display.scroll(“Meow”)
def birthday(self):
self.age = self.age + 1
emmy = Cat(“Emmy”, 3, “Dark brown”)
emmy.birthday()
display.scroll(emmy.age)
4
I have more than 1 cat!
Emmy has a little sister, Saphira! Let’s add her to our code too!
cat1 = Cat(“Emmy”, 3, “Dark brown”)
cat2 = Cat(“Saphira”, 1, “Grey”)
Cat Crime!
There has been a cat crime!
One of the cats has gotten on the kitchen counter and eaten some of my lunch!
They both look innocent but they left a hair behind at the scene of the crime! Let’s write some code to work out who did it
Cat Crime
Who did it??
cat1 = Cat(“Emmy”, 3, “Dark brown”)
cat2 = Cat(“Saphira”, 1, “Grey”)
hair_colour = “Grey”
if hair_colour == cat1.colour:
display.scroll(“That hair belongs to”, cat1.name)
elif hair_colour == cat2.colour:
display.scroll(“That hair belongs to”, cat2.name)
Cat Crime
Who did it??
That hair belongs to Saphira
cat1 = Cat(“Emmy”, 3, “Dark brown”)
cat2 = Cat(“Saphira”, 1, “Grey”)
hair_colour = “Grey”
if hair_colour == cat1.colour:
display.scroll(“That hair belongs to”, cat1.name)
elif hair_colour == cat2.colour:
display.scroll(“That hair belongs to”, cat2.name)
Project time!
You now know all about classes!
�Let’s put what we learnt into our project
Try to do Parts 4-6
The tutors will be around to help!
Micro:Bit Radio
We can use the radio to talk to each other!
All of your Micro:bits have the ability to send and receive radio messages. We are going to use this to make our Micro:bits communicate.
To send radio messages, our Micro:bits send out special invisible light waves at different times to symbolise a series of 1s and 0s, which other Micro:bits can then translate into words and information.
Radio
Your Micro:Bit can send messages to other Micro:Bits using radio waves!
It only takes a few lines of code to make this work!
import radio
radio.on()
radio.send(“Hello World”)
message = radio.receive()
Radio Groups
We need to set our radio to communicate on a certain group, otherwise all our Micro:Bits will try to talk to each other! This will get confusing for the Micro:Bit.
After you turn the radio on, set the group channel!
radio.config(group=100)
Your tutors will give you a group number to use.
Radio Example
What do you think this code does?
Micro:Bit 1 Micro:Bit 2
Why do you think it’s important to check the message?
import radio
radio.on()
radio.config(group=100)
while True:
if button_a.is_pressed():
radio.send(“Hello!”)
if button_b.is_pressed():
radio.send(“World!”)
import radio
radio.on()
radio.config(group=100)
while True:
message = radio.receive()
if message:
display.scroll(message)
Talking over distance…
Everytime our Micro:bits receive a message, it can do a cool thing, where it tells you how strong the signal was. This strength is an indication of how close together the sending and receiving Micro:bits were.
We can do this with:
radio.recieve_full()
This will basically tell the Micro:bit to give you all the information it received from the radio message, instead of just the message.
Although when we use this the message isn't completely readable, so we need to, ignore the first three characters, and convert the rest to a special type of string ("utf8)
Project Time!
Let’s use our MicroBit!
Try Parts 7-9 of your Workbook!�� The tutors will be around to help!