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
Welcome to the Labs
Guess Who!
Girls’ Programming Network
Thank you to our Sponsors!
Platinum Sponsors:
Gold Sponsor:
Girls’ Programming Network
Who are the tutors?
Girls’ Programming Network
Who are you?
Girls’ Programming Network
Introduce your partner
Girls’ Programming Network
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:
Girls’ Programming Network
Tell us you’re here!
Click on the
Start of Day Survey
and fill it in now!
Girls’ Programming Network
Today’s project!
Guess Who?
Girls’ Programming Network
What is Guess Who?
Girls’ Programming Network
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
Introduction to Edstem
Girls’ Programming Network
Log on
Click on your Workbook link to take you into EdStem
Girls’ Programming Network
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:
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
Getting to the lessons
Girls’ Programming Network
The set up of the workbook
The main page:
Girls’ Programming Network
Inside a Chapter
Inside a Chapter there are two main types of pages:
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
How to do the work
In each Lesson there is:
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
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
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
Some shortcuts…
There are a couple things you can do to make copying your code from one page to another easier.
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
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
Intro to Python
Let’s get coding!
Girls’ Programming Network
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
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
Mistakes are great!
Good work you made an error!
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Lists
Girls’ Programming Network
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
When we go shopping, we write down what we want to buy!
Girls’ Programming Network
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
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
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
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
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
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
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
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
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
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
If Statements
Girls’ Programming Network
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
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
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
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
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
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
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
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
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
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
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?
Girls’ Programming Network
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?
Girls’ Programming Network
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
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
Conditions
How about a different number???
fave_num = 9000
if fave_num < 10:
print("that’s a small number")
Girls’ Programming Network
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?
Girls’ Programming Network
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
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
If statements
fave_num = 5
if fave_num < 10:
print("that’s a small number")
This line …
… controls this line
Girls’ Programming Network
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
If statements
word = "GPN"
if word == "GPN":
print("GPN is awesome!")
What happens??
Girls’ Programming Network
If statements
word = "GPN"
if word == "GPN":
print("GPN is awesome!")
What happens??
>>> GPN is awesome!
Girls’ Programming Network
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
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
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
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
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
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
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
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
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
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
For Loops
Girls’ Programming Network
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
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
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
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?
>>> 1
>>> 2
>>> 3
>>> 4
Girls’ Programming Network
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
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
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
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
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
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
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
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
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
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
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
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
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
Random!
Girls’ Programming Network
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
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
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
Project Time!
Raaaaaaaaaandom! Can you handle that?
Try to do the next Part!
The tutors will be around to help!
Girls’ Programming Network
While Loops
Girls’ Programming Network
Loops
We know how to do things on repeat!
Sometimes we want to do some code on repeat!
Girls’ Programming Network
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
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
Introducing … while loops!
while True:
print("Are we there yet?")
Loop condition
Code to repeat
Indent!
Girls’ Programming Network
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
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
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
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
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
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
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
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
Project Time!
while we’re here:
Try to do the next Part
The tutors will be around to help!
Girls’ Programming Network
Extension - Files
Girls’ Programming Network
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
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
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
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
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
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
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
Tell us what you think!
Click on the
End of Day Form
and fill it in now!
Girls’ Programming Network