1 of 159

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!

2 of 159

Welcome to the Labs

Welcome to the labs!

Tamagotchi! - Micro:bits

3 of 159

Thank you to our Sponsors!

Platinum Sponsor:

Gold Sponsor:

4 of 159

Who are the tutors?

5 of 159

Who are you?

6 of 159

Things in Common

  1. Get in a group of 3-5 people

  • Try to find things you all have in common! For example…
    • Does everyone have a brother?
    • Does everyone have a pet cat?
    • Does everyone have the same favourite colour?

  • Write what you have in common on the board.

  • Switch groups, play again!�

7 of 159

Log on

Log on and jump on the GPN website

girlsprogramming.network/workshop

Click on your location

8 of 159

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!

9 of 159

Log on

Click on your Room picture

You can see:

  • A link to the Workbook
  • These Slides (to take a look back on or go on ahead)
  • Other helpful bits like a Cheatsheet to help you code

10 of 159

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

  1. Start by doing this part
  2. Then you can do this part

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’)

11 of 159

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.

12 of 159

Today’s project!

Tamagotchi - Micro:Bit

13 of 159

Tamagotchi

  • You’re going to make your own Tamagotchi electronic pet using a micro:bit

  • Tamagotchi pets were a worldwide fad created in Japan in 1996

  • Give your pet a name and write some code to feed it, play with it and let it sleep

  • Don’t let it get hungry, bored or sleepy!

  • Keep it alive, watch it grow and change

14 of 159

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:

https://microbit.org/

15 of 159

Intro to Micro:Bit

16 of 159

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

17 of 159

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

18 of 159

Using python.microbit.org

Today we will be using python.microbit.org to program our Micro:Bits.

You should see this page pop up!

19 of 159

python.microbit.org

This is where we code

This is the simulator where we test our code

20 of 159

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 *

21 of 159

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

22 of 159

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!

23 of 159

Using the Simulator

  • Click the arrow on the Simulator to run the code
  • A heart is displayed for 1 second and then ‘Hello’

Restart

Stop

We can run our code on the Simulator or the real micro:bit!

Stop, Restart, Simulator settings are underneath

24 of 159

Connect the Micro:Bit

  • Tutors will hand out the micro:bits & cables

  • Connect the small end of the cable to the top of micro:bit

  • Connect the other end to computer USB port

  • New micro:bits will play a “Meet the Microbit” program for you to follow:
    • Push the buttons
    • Shake
    • Tilt to catch flashing LED
    • Clap a few times
  • The tutors will help you

25 of 159

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

  1. Make sure your Micro:Bit is plugged into your computer
  2. Click bottom left
  3. Follow the prompts
  4. Choose your micro:bit and click CONNECT
  5. Wait for the red light on the back of your micro:bit to stop flashing
  6. Your code should be running on the micro:bit!

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

26 of 159

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

  1. Make sure your Micro:Bit is plugged into your computer
  2. Click bottom left
  3. Click Close when you get a popup
  4. Name your project and click Confirm and Save
  5. Follow the instructions on the popup (drag the file from your downloads folder to the MICROBIT device)
  6. Wait for the red light on the back of your micro:bit to stop flashing
  7. Your code should be running on the micro:bit!

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

27 of 159

Mistakes are Great! Errors on the Micro:bit!

  • Programmers make A LOT of errors!
  • Error messages give us hints on how to fix the problem
  • Mistakes don’t break computers!

  • Lots of unexpected words on the micro:bit is an error message
  • Run on the simulator to see it better

I ❤️ errors!

28 of 159

We can learn from our mistakes!

  • In your code - red dot at the start of the line
  • Put the cursor over than line of code to get a hint

2. What went wrong

1. Where the error is

29 of 159

Project Time!

Let’s use our MicroBit!

Try Parts 0 & 1 of your Workbook!�� The tutors will be around to help!

30 of 159

Variables

31 of 159

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

32 of 159

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

33 of 159

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?

34 of 159

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?

35 of 159

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?

36 of 159

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?

37 of 159

While Loops

38 of 159

Loops

We know how to do things on repeat!

Sometimes we want to do some code on repeat!

39 of 159

Introducing … while loops!

What do you think this does?

i = 0

while i < 3:

display.scroll(i)

i = i + 1

40 of 159

Introducing … while loops!

What do you think this does?

i = 0

while i < 3:

display.scroll(i)

i = i + 1

41 of 159

Introducing … while loops!

Stepping through a while loop...

42 of 159

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

43 of 159

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 !

44 of 159

Introducing … while loops!

One step at a time!

i = 0

while i < 3:

display.scroll(i)

i = i + 1

i = 0

MY VARIABLES

Print !

45 of 159

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 !

46 of 159

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 !

47 of 159

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 !

48 of 159

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 !

49 of 159

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 !

50 of 159

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 !

51 of 159

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 !

52 of 159

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 !

53 of 159

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 !

54 of 159

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 !

55 of 159

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!

56 of 159

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

57 of 159

What happens when…..

What happens if we forget to update the loop variable?

i = 0

while i < 3:

display.scroll(i)

58 of 159

What happens when…..

What happens if we forget to update the loop variable?

i = 0

while i < 3:

display.scroll(i)

59 of 159

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?")

60 of 159

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?

61 of 159

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!

62 of 159

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!

63 of 159

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')

64 of 159

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!

65 of 159

Micro:Bit Instructions

66 of 159

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)

67 of 159

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

68 of 159

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.

69 of 159

Micro:Bit Inputs

70 of 159

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():

71 of 159

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)

72 of 159

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

73 of 159

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

74 of 159

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

75 of 159

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():

76 of 159

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()

77 of 159

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()

78 of 159

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()

79 of 159

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

80 of 159

Accelerometer!

What do you think this code does?

while True:

if accelerometer.was_gesture('shake'):

display.scroll('I’m getting dizzy')

81 of 159

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')

82 of 159

Project Time!

Let’s use our MicroBit!

Try Parts 2 & 3 of your Workbook!�� The tutors will be around to help!

83 of 159

Functions!

Simpler, less repetition, easier to read code!

84 of 159

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

85 of 159

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!

86 of 159

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!

87 of 159

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

88 of 159

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

89 of 159

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

90 of 159

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"

91 of 159

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"

92 of 159

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"

93 of 159

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"

94 of 159

Defining your own functions

Built in functions are great! But sometimes we want custom functions!

Defining our own functions means:�

  • We cut down on repeated code�
  • Nice function names makes our code clear and easy to read�
  • We can move bulky code out of the way

95 of 159

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()

96 of 159

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!

97 of 159

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')

98 of 159

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')

99 of 159

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')

100 of 159

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)

101 of 159

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

102 of 159

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

103 of 159

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)

104 of 159

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)

105 of 159

Recap: A function signature

def add(x, y):���add(2, 3)

the def keyword

function name

function arguments

function name

function arguments

definition

callsite

106 of 159

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

107 of 159

Return Keyword

Here’s an example

What's displayed?

def get_name():

return “Alex”

def hello()

name = get_name()

display.scroll(“Hi ” +name)

108 of 159

Return Keyword

Here’s an example

What's displayed?

def get_name():

return “Alex”

def hello()

name = get_name()

display.scroll(“Hi ” +name)

109 of 159

Classes

110 of 159

What is an object?

What do you think an object is?

111 of 159

What is an object?

What do you think an object is?

112 of 159

What is an object?

What do you think an object is?

113 of 159

What is an object?

What do you think an object is?

114 of 159

What is an object?

What do you think an object is?

115 of 159

What is an object?

What do you think an object is?

116 of 159

What is an object in code?

An object is something that we know information about and that can sometimes do things

117 of 159

What is an object in code?

An object is something that we know information about and that can sometimes do things

Like a cat!

118 of 159

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?

119 of 159

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

120 of 159

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

121 of 159

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

122 of 159

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

123 of 159

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

124 of 159

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 #

125 of 159

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?

126 of 159

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

127 of 159

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

128 of 159

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

129 of 159

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

130 of 159

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

131 of 159

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

132 of 159

What does that look like in Python?

Let’s have a look at how we might make a Cat object in Python code!

133 of 159

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

134 of 159

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

135 of 159

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

136 of 159

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

137 of 159

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”)

138 of 159

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)

139 of 159

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

140 of 159

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!

141 of 159

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”)

142 of 159

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()

143 of 159

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

144 of 159

What else can it do?

Let’s have our cat have a Birthday that makes it get older by 1 year!

145 of 159

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

146 of 159

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)

147 of 159

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

148 of 159

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”)

149 of 159

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

150 of 159

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)

151 of 159

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)

152 of 159

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!

153 of 159

Micro:Bit Radio

154 of 159

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.

155 of 159

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!

  1. We have to tell the Micro:Bit that we want to use the radio:

  1. We need to turn the Radio on:

  1. We need to send a message:

  1. We want to receive a message:

import radio

radio.on()

radio.send(“Hello World”)

message = radio.receive()

156 of 159

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.

157 of 159

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)

158 of 159

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)

159 of 159

Project Time!

Let’s use our MicroBit!

Try Parts 7-9 of your Workbook!�� The tutors will be around to help!