1 of 133

Welcome to GPN!

Perth Term 2 2026

Connect to UWA WiFI

1. Select UnifiGuest wireless network�2. Enter the username and password

Username: perthgpn

Password: Gt88.mk?

3. Accept the Terms of Use.�4. Click the "Log in" button.

Connect to UWA WiFi

Girls’ Programming Network

2 of 133

Welcome to the Labs

Guess Who!

Girls’ Programming Network

3 of 133

Thank you to our Sponsors!

Platinum Sponsors:

Gold Sponsor:

Girls’ Programming Network

4 of 133

Who are the tutors?

Girls’ Programming Network

5 of 133

Who are you?

Girls’ Programming Network

6 of 133

Introduce your partner

  1. Find a partner (someone you’ve never met before)
  2. Find out:
    1. Their name
    2. What (school) year they are in
    3. A fun fact about them!
  3. Introduce them to the rest of the group!�

Girls’ Programming Network

7 of 133

Log on

Log on and jump on the GPN website

girlsprogramming.network/workshop

Click on your node location

Click on your room.

From this page you can see:

  • These slides (to take a look back or go on ahead).
  • A link to your workbook in EdStem
  • Other helpful bits to use through the day!

Girls’ Programming Network

8 of 133

Tell us you’re here!

Click on the

Start of Day Survey

and fill it in now!

Girls’ Programming Network

9 of 133

Today’s project!

Guess Who?

Girls’ Programming Network

10 of 133

What is Guess Who?

  • Fun Board Game
  • We’re going to code our own version
  • You pick a character, keep is secret!
  • The computer will ask you questions to figure out who it is

Girls’ Programming Network

11 of 133

What is Guess Who?

Q: Do they have a hat?

A: YES

Eliminates everyone without a hat

Keep asking questions until figure out who it is

X

X

X

X

X

X

X

X

Girls’ Programming Network

12 of 133

Introduction to Edstem

Girls’ Programming Network

13 of 133

Log on

Click on your Workbook link to take you into EdStem

Girls’ Programming Network

14 of 133

Signing up to Edstem

Log in if you already have a an “Edstem” account from a past GPN

If you haven’t got an account, let’s make one:

  1. Type in your Full Name
  2. Type in your personal email
  3. Click Create Account
  4. Go to your email and verify your new account
  5. Create a password

Click Join Course

The name of your course will be at the top :

If you don’t have access to your email account, ask a tutor for a GPN Edstem login

Girls’ Programming Network

15 of 133

Getting to the lessons

  1. Once you are in the course, you’ll be taken to a discussion page.
  2. Click the button for the lessons page (top right - looks like a book)

Girls’ Programming Network

16 of 133

The set up of the workbook

The main page:

  1. Heading at the top that tells you the project you are in

  1. List of “Chapters” called something like 1:Welcome Message They have an icon that looks like this:

  1. To complete your project, work through the chapters one at a time

Girls’ Programming Network

17 of 133

Inside a Chapter

Inside a Chapter there are two main types of pages:

  • Lessons where you will do your coding.
    • They have this icon:

  • Checkpoints

Each chapter has a checkpoint to complete to move to the next chapter. Make sure you scroll down to see all the questions in a checkpoint.

There may also be Bonus Lessons to try if you want to or if you are waiting for the next lecture

Girls’ Programming Network

18 of 133

How to do the work

In each Lesson there is:

  1. A section on the left with instructions
  2. A section on the right for your code

You will need to copy your code from the last lesson, then follow the instructions to change your code

There are also Hints and Code Blocks to help you

Girls’ Programming Network

19 of 133

Hints

Sometimes in a lesson, there’s some code we want you to do that might be a bit tricky, to help you out we’ve added some hints. They look like this:

If you press the blue run button it will show you what that code does, you can even change the code to see if/how it changes.

These are just hints make sure you’re not copying the hint into your code as it will likely end up breaking. They are just to show you the kinds of things you can do.

Girls’ Programming Network

20 of 133

Running your code…

Click in the bottom right hand corner

Your code will run and any output will display in the Console

Don’t worry if you forget. Tutors

will help!

Girls’ Programming Network

21 of 133

Some shortcuts…

There are a couple things you can do to make copying your code from one page to another easier.

  1. Ctrl + A

  1. Ctrl + C

  1. Ctrl + V

Pressing these keys together will select all the text on a page

Pressing these keys together will copy anything that’s selected

Pressing these keys together will paste anything you’ve copied

On Macs use Command (⌘) instead of Ctrl

Girls’ Programming Network

22 of 133

Project time!

You now know all about the EdStem!

You should now sign up and join our EdStem class if you haven’t done this already.

Remember the tutors will be around to help!

Girls’ Programming Network

23 of 133

Intro to Python

Let’s get coding!

Girls’ Programming Network

24 of 133

Let’s make a mistake!

Click on Chapter 1 “Welcome Message”

The Print a Welcome Message Lesson opens.

It looks like this

Girls’ Programming Network

25 of 133

Let’s make a mistake!

Type by button mashing the keyboard here - type anything you want

Click Run here to run your code!

Did you get a big ugly error message?

Girls’ Programming Network

26 of 133

Mistakes are great!

Good work you made an error!

  • Programmers make A LOT of errors!
  • Errors give us hints to find mistakes
  • Run your code often to get the hints!!
  • Mistakes won’t break computers!

AttributeError: 'NoneType' object has no attribute 'foo'

TypeError: Can't convert 'int' object to str implicitly

ImportError: No module named humour

KeyError: ‘Hairy Potter’

SyntaxError: Invalid Syntax

Girls’ Programming Network

27 of 133

We can learn from our mistakes!

Error messages help us fix our mistakes!

We read error messages from bottom to top

Traceback (most recent call last):

File "C:/Users/Madeleine/Desktop/tmp.py", line 9, in <module>

print("I have " + 5 + " apples")

TypeError: can only concatenate str (not "int") to str

1. What went wrong

3. Where that code is

2. What code didn’t work

Girls’ Programming Network

28 of 133

Adding a comment!

Sometimes we want to write things in our file that the computer doesn’t look at. We can use comments for that! �Sometimes we want to write a note for a people to read

And sometimes we want to not run some code (but don’t want to delete it!)

# This code was written by Vivian

# print(“Goodbye world!”)

Girls’ Programming Network

29 of 133

Tell me more!

We can print things in lots of different ways in python!

>>> print("Hello world!")

Hello world!

>>> print("Hello", "world!")

Hello world!

>>> print("Hello" + "world")

Hello world!

Note that this last one will not have a new line after it!

Girls’ Programming Network

30 of 133

Tell me more!

We can print things in lots of different ways in python!

>>> print("Hello world!")

Hello world!

>>> print("Hello", "world!")

Hello world!

>>> print("Hello" + "world")

Hello world!

Note that this last one will not have a new line after it!

Girls’ Programming Network

31 of 133

Tell me more!

We can print things in lots of different ways in python!

>>> print("Hello world!")

Hello world!

>>> print("Hello", "world!")

Hello world!

>>> print("Hello" + "world")

Hello world!

Note that this last one will not have a new line after it!

When we use a comma it prints with a space between the words

Girls’ Programming Network

32 of 133

Tell me more!

We can print things in lots of different ways in python!

>>> print("Hello world!")

Hello world!

>>> print("Hello", "world!")

Hello world!

>>> print("Hello" + "world")

Hello world!

Note that this last one will not have a new line after it!

Hello world!

When we use a comma it prints with a space between the words

Girls’ Programming Network

33 of 133

Tell me more!

We can print things in lots of different ways in python!

>>> print("Hello world!")

Hello world!

>>> print("Hello", "world!")

Hello world!

>>> print("Hello" + "world")

Hello world!

Note that this last one will not have a new line after it!

Hello world!

When we use a + it just adds the words together - no spaces

Girls’ Programming Network

34 of 133

Tell me more!

We can print things in lots of different ways in python!

>>> print("Hello world!")

Hello world!

>>> print("Hello", "world!")

Hello world!

>>> print("Hello" + "world")

Helloworld!

Hello world!

When we use a + it just adds the words together - no spaces

Girls’ Programming Network

35 of 133

Tell me more!

We can print on many lines at once!

>>> print("""Hello world.

This is me!

Life should be fun for everyone""")

Hello world.

This is me!

Life should be fun for everyone

Girls’ Programming Network

36 of 133

Tell me more!

We can print on many lines at once!

>>> print("""Hello world.

This is me!

Life should be fun for everyone""")

Hello world.

This is me!

Life should be fun for everyone

Girls’ Programming Network

37 of 133

Variables

When coding, we can make a variable called friend and set it to a value like this

friend = “Alex”

friend

Alex

Girls’ Programming Network

38 of 133

Variables

Instead of writing the word “Alex”, we can write friend (the variable’s name).

The computer will substitute the current value of the variable.

It’s like we’re getting the value out of the box!

print(friend)

friend

Alex

Alex

Girls’ Programming Network

39 of 133

Reusing variables

We can replace values in variables:

animal = "dog"

print("My favourite animal is a " + animal)

animal = "cat"

print("My favourite animal is a " + animal)

animal = animal + "dog"

print("My favourite animal is a " + animal)

My favourite animal is a dog

My favourite animal is a cat

My favourite animal is a catdog

What will this output?

Girls’ Programming Network

40 of 133

Asking a question!

my_name = input('What is your name? ')

print('Hello ' + my_name)

What do you think happens?

Store the answer in the variable my_name

Writing input tells the computer to wait for a response

This is the question you want printed to the screen

Girls’ Programming Network

41 of 133

Asking a question!

my_name = input('What is your name? ')

print('Hello ' + my_name)

What do you think happens?

What is your name? Maddie

Hello Maddie

Store the answer in the variable my_name

Writing input tells the computer to wait for a response

This is the question you want printed to the screen

We can use the answer the user wrote that we then stored later!

Girls’ Programming Network

42 of 133

Project time!

Guess Who!�

Now that we’ve had a Python refresher, try and do Part0 and 1 !

The tutors will be around to help!

Girls’ Programming Network

43 of 133

Lists

Girls’ Programming Network

44 of 133

Lists

But we don’t store it on lots of little pieces of paper!

We put it in one big shopping list!

��

Bread

Ice Cream

Chocolate

Pizza

  • Bread
  • Chocolate
  • Ice Cream
  • Pizza

When we go shopping, we write down what we want to buy!

Girls’ Programming Network

45 of 133

Lists

It would be annoying to store it separately when we code too

>>> shopping_item1 = "Bread"

>>> shopping_item2 = "Chocolate"

>>> shopping_item3 = "Ice Cream"

>>> shopping_item4 = "Pizza"

So much repetition!�

Instead we use a python list!

>>> shopping_list = ["Bread", "Chocolate", "Ice Cream", "Pizza"]

Girls’ Programming Network

46 of 133

List anatomy

shopping_list = ["Bread", "Chocolate", "Ice Cream", "Pizza"]

Stored in the variable shopping_list

Has square brackets

Made up of different items (these are strings)

The items are separated by commas

Girls’ Programming Network

47 of 133

Accessing Lists!

The favourites list holds four strings in order.

We can count out the items using index numbers!

0 1 2 3

Remember: Indices start from zero!

Girls’ Programming Network

48 of 133

Accessing Lists

We access the items in a list with an index such as [0]:

>>> faves[0]

'books'

What code do you need to access the second item in the list?

>>> faves[1]

'butterfly'

0 [1] 2 3

Girls’ Programming Network

49 of 133

Accessing Lists

We access the items in a list with an index such as [0]:

>>> faves[0]

'books'

What code do you need to access the second item in the list?

>>> faves[1]

'butterfly'

0 [1] 2 3

Girls’ Programming Network

50 of 133

Falling off the edge

Python complains if you try to go past the end of a list

>>> faves = ['books', 'butterfly', 'chocolate',

'skateboard']

>>> faves[4]

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

IndexError: list index out of range

Girls’ Programming Network

51 of 133

Removing items!

We can remove items from the list if they’re no longer needed!

What if we decided that we didn’t like butterflies anymore?

>>> faves

['books', 'butterfly', 'lollipops', 'skateboard']

>>> faves.remove('butterfly')

What does this list look like now?

['books', 'lollipops', 'skateboard']

Girls’ Programming Network

52 of 133

Removing items!

We can remove items from the list if they’re no longer needed!

What if we decided that we didn’t like butterflies anymore?

>>> faves

['books', 'butterfly', 'lollipops', 'skateboard']

>>> faves.remove('butterfly')

What does this list look like now?

['books', 'lollipops', 'skateboard']

Girls’ Programming Network

53 of 133

List of lists!

You really can put anything in a list, even more lists!

We could use a list of lists to store different sports teams!

tennis_pairs = [

["Alex", "Emily"], ["Kass", "Annie"], ["Amara", "Viv"]

]

Get the first pair in the list

>>> first_pair = tennis_pairs[0]

>>> ["Alex", "Emily"]

Now we have the first pair handy, we can get the first the first player of the first pair

>>> fist_player = first_pair[0]

>>> "Alex"

Girls’ Programming Network

54 of 133

Project time!

You now know all about lists!

Let’s put what we learnt into our project

Try to do Part 2

The tutors will be around to help!

Girls’ Programming Network

55 of 133

If Statements

Girls’ Programming Network

56 of 133

Conditions!

Conditions let us make decision.

First we test if the condition is met!

Then maybe we’ll do the thing

If it’s raining take an umbrella

Yep it’s raining

…... take an umbrella

Girls’ Programming Network

57 of 133

Booleans (True and False)

Computers store whether a condition is met in the form of �True and False

To figure out if something is True or False we do a comparison

"Dog" == "dog"

"D" in "Dog"

"Q" not in "Cat"

5 < 10

3 + 2 == 5

5 != 5

Girls’ Programming Network

58 of 133

Booleans (True and False)

Computers store whether a condition is met in the form of �True and False

To figure out if something is True or False we do a comparison

"Dog" == "dog"

"D" in "Dog"

"Q" not in "Cat"

5 < 10

3 + 2 == 5

5 != 5

True

Girls’ Programming Network

59 of 133

Booleans (True and False)

Computers store whether a condition is met in the form of �True and False

To figure out if something is True or False we do a comparison

"Dog" == "dog"

"D" in "Dog"

"Q" not in "Cat"

5 < 10

3 + 2 == 5

5 != 5

True

True

Girls’ Programming Network

60 of 133

Booleans (True and False)

Computers store whether a condition is met in the form of �True and False

To figure out if something is True or False we do a comparison

"Dog" == "dog"

"D" in "Dog"

"Q" not in "Cat"

5 < 10

3 + 2 == 5

5 != 5

True

True

False

Girls’ Programming Network

61 of 133

Booleans (True and False)

Computers store whether a condition is met in the form of �True and False

To figure out if something is True or False we do a comparison

"Dog" == "dog"

"D" in "Dog"

"Q" not in "Cat"

5 < 10

3 + 2 == 5

5 != 5

True

True

False

False

Girls’ Programming Network

62 of 133

Booleans (True and False)

Computers store whether a condition is met in the form of �True and False

To figure out if something is True or False we do a comparison

"Dog" == "dog"

"D" in "Dog"

"Q" not in "Cat"

5 < 10

3 + 2 == 5

5 != 5

True

True

False

False

True

Girls’ Programming Network

63 of 133

Booleans (True and False)

Computers store whether a condition is met in the form of �True and False

To figure out if something is True or False we do a comparison

"Dog" == "dog"

"D" in "Dog"

"Q" not in "Cat"

5 < 10

3 + 2 == 5

5 != 5

True

True

False

False

True

True

Girls’ Programming Network

64 of 133

Conditions

So to know whether to do something, they find out if it’s True!

fave_num = 5

if fave_num < 10:

print("that’s a small number")

Girls’ Programming Network

65 of 133

Conditions

So to know whether to do something, they find out if it’s True!

fave_num = 5

if fave_num < 10:

print("that’s a small number")

That’s the condition!

Girls’ Programming Network

66 of 133

Conditions

So to know whether to do something, they find out if it’s True!

fave_num = 5

if fave_num < 10:

print("that’s a small number")

That’s the condition!

Is it True that fave_num is less than 10?

  • Well, fave_num is 5
  • And it’s True that 5 is less than 10
  • So it is True!

Girls’ Programming Network

67 of 133

Conditions

So to know whether to do something, they find out if it’s True!

fave_num = 5

if fave_num < 10:

print("that’s a small number")

True

Put in the answer to the question

Is it True that fave_num is less than 10?

  • Well, fave_num is 5
  • And it’s True that 5 is less than 10
  • So it is True!

Girls’ Programming Network

68 of 133

Conditions

So to know whether to do something, they find out if it’s True!

fave_num = 5

if fave_num < 10:

print("that’s a small number")

True

What do you think happens?>>>

Girls’ Programming Network

69 of 133

Conditions

So to know whether to do something, they find out if it’s True!

fave_num = 5

if fave_num < 10:

print("that’s a small number")

True

What do you think happens?>>> that’s a small number

Girls’ Programming Network

70 of 133

Conditions

How about a different number???

fave_num = 9000

if fave_num < 10:

print("that’s a small number")

Girls’ Programming Network

71 of 133

Conditions

Find out if it’s True!

fave_num = 9000

if fave_num < 10:

print("that’s a small number")

False

Put in the answer to the question

Is it True that fave_num is less than 10?

  • Well, fave_num is 9000
  • And it’s not True that 9000 is less than 10
  • So it is False!

Girls’ Programming Network

72 of 133

Conditions

How about a different number???

fave_num = 9000

if fave_num < 10:

print("that’s a small number")

What do you think happens?>>>

Girls’ Programming Network

73 of 133

Conditions

How about a different number???

fave_num = 9000

if fave_num < 10:

print("that’s a small number")

What do you think happens?>>>

Nothing!

Girls’ Programming Network

74 of 133

If statements

fave_num = 5

if fave_num < 10:

print("that’s a small number")

This line …

… controls this line

Girls’ Programming Network

75 of 133

If statements

fave_num = 5

if fave_num < 10:

print("that’s a small number")

print("and I like that")

print("A LOT!!")

This line …

… controls anything below it that is indented like this!

Actually …..

Girls’ Programming Network

76 of 133

If statements

word = "GPN"

if word == "GPN":

print("GPN is awesome!")

What happens??

Girls’ Programming Network

77 of 133

If statements

word = "GPN"

if word == "GPN":

print("GPN is awesome!")

What happens??

>>> GPN is awesome!

Girls’ Programming Network

78 of 133

Else statements

word = "GPN"

if word == "GPN":

print("GPN is awesome!")

What happens??

>>> GPN is awesome!

But what if we want something different to happen if the word isn’t "GPN"

Girls’ Programming Network

79 of 133

Else statements

word = "Chocolate"

if word == "GPN":

print("GPN is awesome!")

else:

print("The word isn’t GPN :(")

What happens??

else

Statements means something still happens if the if statement was False

Girls’ Programming Network

80 of 133

Else statements

word = "Chocolate"

if word == "GPN":

print("GPN is awesome!")

else:

print("The word isn’t GPN :(")

What happens??

>>> The word isn’t GPN :(

else

Statements means something still happens if the if statement was False

Girls’ Programming Network

81 of 133

Elif statements

word = "Chocolate"

if word == "GPN":

print("GPN is awesome!")

elif word == "Chocolate":

print("YUMMM Chocolate!")

else:

print("The word isn’t GPN :(")

What happens??

elif

Means we can give specific instructions for other words

Girls’ Programming Network

82 of 133

Elif statements

word = "Chocolate"

if word == "GPN":

print("GPN is awesome!")

elif word == "Chocolate":

print("YUMMM Chocolate!")

else:

print("The word isn’t GPN :(")

What happens??

>>> YUMMM Chocolate!

elif

Means we can give specific instructions for other words

Girls’ Programming Network

83 of 133

Simple Conditions!

We’ve learned about simple conditions like this one before.

They’re really useful when you only want something to happen sometimes.

weather = "raining"

if weather == "raining":

print("Take an umbrella!")

Girls’ Programming Network

84 of 133

Complex Conditions!

But what if you want to only take an umbrella if it’s raining and you’re going outside?

You might do it like this:

weather = "raining"

location = "outside"

if weather == "raining":

if location == "outside":

print("Take an umbrella!")

Girls’ Programming Network

85 of 133

Complex Conditions!

But what if you want to only take an umbrella if it’s raining and you’re go outside?

You might do it like this:

But that starts to get messy quickly.

weather = "raining"

location = "outside"

if weather == "raining":

if location == "outside":

print("Take an umbrella!")

Girls’ Programming Network

86 of 133

AND

Instead you can do it like this!

This is easier to read and stops things getting messy, especially if you have lots of conditions to check.

weather = "raining"

location = "outside"

if weather == "raining" and location == "outside":

print("Take an umbrella!")

Girls’ Programming Network

87 of 133

Project Time!

You now know all about if and else!

See if you can do the next part

The tutors will be around to help!

Girls’ Programming Network

88 of 133

For Loops

Girls’ Programming Network

89 of 133

Looping through lists!

What would we do if we wanted to print out this list, one word at a time?

What if it had a 100 items??? That would be BORING!��

words = ['This', 'is', 'a', 'sentence']

print(words[0])

print(words[1])

print(words[2])

print(words[3])

Girls’ Programming Network

90 of 133

For Loops

For loops allow you to do something for each item in a group of things

There are many real world examples, like:

For each page in this book:

Read page�

For each chip in this bag of chips:

Eat chip�

Girls’ Programming Network

91 of 133

Looping over a list of ints

We can loop through a list:

numbers = [1, 2, 3, 4]

for i in numbers:

print(i)

What’s going to happen?

Girls’ Programming Network

92 of 133

Looping over a list of ints

We can loop through a list:

  • Each item of the list takes a turn at being the variable i
  • Do the body once for each item
  • We’re done when we run out of items!

numbers = [1, 2, 3, 4]

for i in numbers:

print(i)

What’s going to happen?

>>> 1

>>> 2

>>> 3

>>> 4

Girls’ Programming Network

93 of 133

Looping over a list of ints

Strings are lists of letters!

word = "cat"

for i in word:

print(i)

What’s going to happen?

Girls’ Programming Network

94 of 133

Looping over a list of ints

Strings are lists of letters!

word = "cat"

for i in word:

print(i)

What’s going to happen?

>>> c

>>> a

>>> t

Girls’ Programming Network

95 of 133

How does it work??

Somehow it knows how to get one fruit out at a time!!

It’s like it knows english!

But fruit is just a variable! We could call it anything! Like dog!

fruits = ['apple', 'banana', 'mango']

for fruit in fruits:

print('yummy ' + fruit)

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

>>> Yummy apple�>>> Yummy banana�>>> Yummy mango

Girls’ Programming Network

96 of 133

How does it work??

Everything in the list gets to have a turn at being the dog variable

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

Let’s set dog to to the first thing in the list!

dog is now 'apple'!

Girls’ Programming Network

97 of 133

How does it work??

Everything in the list gets to have a turn at being the dog variable

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

Let’s set dog to to the first thing in the list!

dog is now 'apple'!

print('yummy ' + dog)

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

>>> Yummy apple�

Girls’ Programming Network

98 of 133

How does it work??

Everything in the list gets to have a turn at being the dog variable

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

Let’s set dog to to the first thing in the list!

dog is now 'apple'!

print('yummy ' + dog)

We’re at the end of the loop body, back to the top!

>>> Yummy apple�

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

Girls’ Programming Network

99 of 133

How does it work??

Everything in the list gets to have a turn at being the dog variable

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

Let’s set dog to to the first thing in the list!

dog is now 'apple'!

print('yummy ' + dog)

We’re at the end of the loop body, back to the top!

Let’s set dog to to the next thing in the list!

dog is now 'banana'!

>>> Yummy apple�

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

Girls’ Programming Network

100 of 133

How does it work??

Everything in the list gets to have a turn at being the dog variable

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

Let’s set dog to to the first thing in the list!

dog is now 'apple'!

print('yummy ' + dog)

We’re at the end of the loop body, back to the top!

Let’s set dog to to the next thing in the list!

dog is now 'banana'!

print('yummy ' + dog)

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

>>> Yummy apple

>>> Yummy banana�

Girls’ Programming Network

101 of 133

How does it work??

Everything in the list gets to have a turn at being the dog variable

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

Let’s set dog to to the first thing in the list!

dog is now 'apple'!

print('yummy ' + dog)

We’re at the end of the loop body, back to the top!

Let’s set dog to to the next thing in the list!

dog is now 'banana'!

print('yummy ' + dog)

Out of body, back to the top!

>>> Yummy apple

>>> Yummy banana�

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

Girls’ Programming Network

102 of 133

How does it work??

Everything in the list gets to have a turn at being the dog variable

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

Let’s set dog to to the first thing in the list!

dog is now 'apple'!

print('yummy ' + dog)

We’re at the end of the loop body, back to the top!

Let’s set dog to to the next thing in the list!

dog is now 'banana'!

print('yummy ' + dog)

Out of body, back to the top!��Let’s set dog to to the next thing in the list!�dog is now 'mango'!�

>>> Yummy apple

>>> Yummy banana�

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

Girls’ Programming Network

103 of 133

How does it work??

Everything in the list gets to have a turn at being the dog variable

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

Let’s set dog to to the first thing in the list!

dog is now 'apple'!

print('yummy ' + dog)

We’re at the end of the loop body, back to the top!

Let’s set dog to to the next thing in the list!

dog is now 'banana'!

print('yummy ' + dog)

Out of body, back to the top!��Let’s set dog to to the next thing in the list!�dog is now 'mango'!

print('yummy ' + dog)

>>> Yummy apple

>>> Yummy banana

>>> Yummy mango�

Girls’ Programming Network

104 of 133

How does it work??

Everything in the list gets to have a turn at being the dog variable

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

fruits = ['apple', 'banana', 'mango']

for dog in fruits:

print('yummy ' + dog)

Let’s set dog to to the first thing in the list!

dog is now 'apple'!

print('yummy ' + dog)

We’re at the end of the loop body, back to the top!

Let’s set dog to to the next thing in the list!

dog is now 'banana'!

print('yummy ' + dog)

Out of body, back to the top!��Let’s set dog to to the next thing in the list!�dog is now 'mango'!

print('yummy ' + dog)Out of body, and out of list!!

We’re done here!

>>> Yummy apple

>>> Yummy banana

>>> Yummy mango�

Girls’ Programming Network

105 of 133

Project Time!

Now you know how to use a for loop!

Try to do the next Parts

...if you are up for it!

The tutors will be around to help!

Girls’ Programming Network

106 of 133

Random!

Girls’ Programming Network

107 of 133

That’s so random!

There’s lots of things in life that are up to chance or random!

Python lets us import common bits of code people use! We’re going to use the random module!

We want the computer to be random sometimes!

Girls’ Programming Network

108 of 133

Using the random module

Let’s choose something randomly from a list!

This is like drawing something out of a hat in a raffle!

We use random.choice to randomly select something from a list

Each time we run this we would probably get a different answer

eggs OR bread OR apples OR milk

import random

shopping_list = ["eggs", "bread", "apples", "milk"]

random.choice(shopping_list)

Girls’ Programming Network

109 of 133

Using the random module

You can also assign your random choice to a variable and then use that variable in your code

import random

shopping_list = ["eggs", "bread", "apples", "milk"]

random_food = random.choice(shopping_list)

print(random_food)

The variable random_food contains the random choice that was made from the list

Girls’ Programming Network

110 of 133

Project Time!

Raaaaaaaaaandom! Can you handle that?

Try to do the next Part!

The tutors will be around to help!

Girls’ Programming Network

111 of 133

While Loops

Girls’ Programming Network

112 of 133

Loops

We know how to do things on repeat!

Sometimes we want to do some code on repeat!

Girls’ Programming Network

113 of 133

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

Girls’ Programming Network

114 of 133

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

Are we there yet?

Are we there yet?

Are we there yet?

Are we there yet?

Are we there yet?

Are we there yet?

Are we there yet?

Girls’ Programming Network

115 of 133

Introducing … while loops!

while True:

print("Are we there yet?")

Loop condition

Code to repeat

Indent!

Girls’ Programming Network

116 of 133

Give me a break!

But what if I want to get out of a loop early?

That’s when we use the break keyword!

while True :

number = input("Guess a number: ")

if number == "42":

print("You got it right!")

break

Girls’ Programming Network

117 of 133

Give me a break!

But what if I want to get out of a loop early?

That’s when we use the break keyword!

while True :

number = input("Guess a number: ")

if number == "42":

print("You got it right!")

break

Guess a number:

Girls’ Programming Network

118 of 133

Give me a break!

But what if I want to get out of a loop early?

That’s when we use the break keyword!

while True :

number = input("Guess a number: ")

if number == "42":

print("You got it right!")

break

Guess a number: 5

Girls’ Programming Network

119 of 133

Give me a break!

But what if I want to get out of a loop early?

That’s when we use the break keyword!

while True :

number = input("Guess a number: ")

if number == "42":

print("You got it right!")

break

Guess a number: 5

Guess a number:

Girls’ Programming Network

120 of 133

Give me a break!

But what if I want to get out of a loop early?

That’s when we use the break keyword!

while True :

number = input("Guess a number: ")

if number == "42":

print("You got it right!")

break

Guess a number: 5

Guess a number: 100

Girls’ Programming Network

121 of 133

Give me a break!

But what if I want to get out of a loop early?

That’s when we use the break keyword!

while True :

number = input("Guess a number: ")

if number == "42":

print("You got it right!")

break

Guess a number: 5

Guess a number: 100

Guess a number:

Girls’ Programming Network

122 of 133

Give me a break!

But what if I want to get out of a loop early?

That’s when we use the break keyword!

while True :

number = input("Guess a number: ")

if number == "42":

print("You got it right!")

break

Guess a number: 5

Guess a number: 100

Guess a number: 42

Girls’ Programming Network

123 of 133

Give me a break!

But what if I want to get out of a loop early?

That’s when we use the break keyword!

while True :

number = input("Guess a number: ")

if number == "42":

print("You got it right!")

break

Guess a number: 5

Guess a number: 100

Guess a number: 42

You got it right!

Girls’ Programming Network

124 of 133

Project Time!

while we’re here:

Try to do the next Part

The tutors will be around to help!

Girls’ Programming Network

125 of 133

Extension - Files

Girls’ Programming Network

126 of 133

Filing it away!

What happens if we want to use different data in our program? What if that data is too big to write in with the keyboard?

We’d have to change our code!!

It would be better if we could keep all our data in a file and just be able to pick and choose what file we wanted to play today!

people.txt

Aleisha,brown,black,hat

Brittany,blue,red,glasses

Charlie,green,brown,glasses

Dave,blue,red,glasses

Eve,green,brown,glasses

Frankie,hazel,black,hat

George,brown,black,glasses

Hannah,brown,black,glasses

Isla,brown,brown,none

Jackie,hazel,blonde,hat

Kevin,brown,black,hat

Luka,blue,brown,none

Girls’ Programming Network

127 of 133

Opening files!

To get access to the stuff inside a file in python we need to open it!

That doesn’t mean clicking on the little icon!

with open("test.txt", "r") as f:

You’ll now be able to read the things in f

If your file is in the same location as your code you can just use the name!

Girls’ Programming Network

128 of 133

A missing file causes an error

Here we try to open a file that doesn't exist:

with open("missing.txt", "r") as f:

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

IOError: [Errno 2] No such file or directory: 'missing.txt'

Girls’ Programming Network

129 of 133

You can read in one line at a time

You can use a for loop to read 1 line at a time!

with open("haiku.txt", "r") as f:

for line in f:

print(line)

Wanna go outside.��Oh NO! Help! I got outside!��Let me back inside!

Why is there an extra blank line each time?

Girls’ Programming Network

130 of 133

Chomping off the newline

The newline character is represented by '\n':

print('Hello\nWorld')�Hello�World

We can remove it from the lines we read with .strip()

x = 'abc\n'

x.strip()

'abc'

x.strip() is safe as lines without newlines will be unaffected

Girls’ Programming Network

131 of 133

Reading and stripping!

with open("haiku.txt", "r") as f:

for line in f:

line = line.strip()

print(line)

Wanna go outside.�Oh NO! Help! I got outside!�Let me back inside!

No extra lines!

Girls’ Programming Network

132 of 133

Project time!

I hope you filed that knowledge away

Use it in the next section of the project!

Try to do the next Part

The tutors will be around to help!

Girls’ Programming Network

133 of 133

Tell us what you think!

Click on the

End of Day Form

and fill it in now!

Girls’ Programming Network