Lesson 3:
At a crossroads
Year 8 – Intro to Python programming
Starter activity
What if... .
you wanted the program to recognise one particular name and treat it differently?
print("What’s your name?")
user = input()
print("Hello", user)
Something missing
print("Best film ever?")
film = input()
print(film, "is my favourite too!")
What if... .
you wanted the program to react enthusiastically only to a particular film?
print("Year of birth?")
birth_year = int(input())
age = 2020 - birth_year
print("You are", age, "years old")
What if... .
you wanted the program to display a comment that depends on the user’s age range?
Starter activity
Selection
When your programs check conditions and select the path of action that they will follow accordingly
Objectives
In this lesson, you will...
Activity 1
Selection
if :
else:
condition
block of statements
block of statements
if :
block of statements
condition
Activity 1
Selection
You will need an if or an if, else:
when there is more than one possible path for your program to follow.
if :
else:
condition
block of statements
block of statements
if :
block of statements
condition
Activity 1
Selection in Python: Greeting
Use pair programming .
Driver
Control the keyboard and mouse.
Navigator
Provide support and instructions.
Alternate between roles.
Activity 1
print("What’s your name?")
user = input()
print("Hello", user)
Selection in Python: Greeting
Extend this program to recognise one particular name and treat it differently.
Live coding (ncce.io/py-greeting-30)
Activity 1
Selection in Python: commentary
print("What’s your name?")
user = input()
if user == "Elizabeth":
print("Good morning Your Majesty")
else:
print("Hello", user)
The condition will check if the value of user is equal to the string "Elizabeth".
This is the if block, i.e. the code that will be executed if the condition is True.
The expression user == "Elizabeth" will evaluate to either True or False.
This is the else block, i.e. the code that will be executed if the condition is False.
Only one of these blocks will be executed, depending on the value of the condition.
Activity 1
Selection in Python: beware of syntax
if user == "Elizabeth":
print("Good morning Your Majesty")
else:
print("Hello", user)
Syntax pitfalls .
It’s if and else. No capitals.
✔
A colon : is always required after the
if condition and after else.
✔
The == operator checks for equality.
The single = is only used in assignments.
✔
Use indentation to indicate which statements ‘belong’ to the if block and the else block.
✔
user is a variable. Don’t use quotes.
"Elizabeth" is a string literal. It needs quotes.
✔
Activity 1
Relational operators in Python (comparisons)
You can use these operators to compare the values of expressions.
Expressions formed using these operators evaluate to either True or False.
== equal to
!= not equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to
Examples
a == 1 Does a equal 1?
b != c Are b and c different?
d < 3 Is d less than 3?
d <= 3 Is d at most 3?
d > 10 Is d greater than 10?
d >= 10 Is d at least 10?
You can also use these operators to compare alphanumeric values (strings).
Activity 2
Practise using selection
Extend some of the programs that we’ve built so far to use selection.
Use your worksheet.
Activity 2
print("Best film ever?")
film = input()
if film != "BFG":
print(film, "is not too bad")
else:
print(film, "is my favourite too!")
Practise using selection: example solutions
Activity 2
lucky = 13
print("Guess my number:")
guess = int(input())
if guess == lucky:
print("Amazing, you guessed it")
else:
print("Sorry, it’s not", guess)
print("My lucky number is", lucky)
print("Nice playing with you")
Practise using selection: example solutions
Activity 3
Lucky number: labels
Pick a lucky number .
Say goodbye .
Check answer and provide feedback .
lucky = 13
print("Guess my number:")
guess = int(input())
if guess == lucky:
print("Amazing, you guessed it")
else:
print("Sorry, it’s not", guess)
print("My lucky number is", lucky)
print("Nice playing with you")
Prompt the user to guess .
Activity 3
lucky = 13
print("Guess my number:")
guess = int(input())
if guess == lucky:
print("Amazing, you guessed it")
else:
print("Sorry, it’s not", guess)
print("My lucky number is", lucky)
print("Nice playing with you")
Lucky number: executing the program
Amazing, you guessed it
Input/Output .
lucky
13
guess
13
State .
Nice playing with you
True
Guess my number:
User types 13
Activity 3
lucky = 13
print("Guess my number:")
guess = int(input())
if guess == lucky:
print("Amazing, you guessed it")
else:
print("Sorry, it’s not", guess)
print("My lucky number is", lucky)
print("Nice playing with you")
Sorry, it’s not 7
lucky
13
guess
7
State .
My lucky number is 13
Nice playing with you
Lucky number: executing the program
False
Input/Output .
Guess my number:
User types 7
Activity 4
Randomness
What if... .
you wanted a program that didn’t always pick the same lucky number?
lucky = 13
print("Guess my number:")
guess = int(input())
if guess == lucky:
print("Amazing, you guessed it")
else:
print("Sorry, it’s not", guess)
print("My lucky number is", lucky)
print("Nice playing with you")
Live coding (ncce.io/py-lucky-31)
Activity 4
from random import randint
lucky = randint(1,20)
print("Guess my number:")
guess = int(input())
if guess == lucky:
print("Amazing, you guessed it")
else:
print("Sorry, it’s not", guess)
print("My lucky number is", lucky)
print("Nice playing with you")
Function call: Invoke the randint function, to return a random integer.
Randomness
Modules (or libraries) .
They extend what our programs can do by providing additional functions. .
Importing: “from the random module, the program will need the randint function”
The (comma-separated) arguments 1 and 20 specify the range for the random integer.
Activity 4
Question .
You are making a dice rolling game. Fill in the gap to complete the program.
1
2
3
4
▹
Randomness
from random import randint
diceroll =
print("You rolled a", diceroll)
Activity 4
Question .
When this program is executed, what will its output be?
1
2
3
4
▹
Randomness
from random import randint
diceroll = randint(1,6)
print("You rolled a", diceroll)
Activity 4
Randomness
from random import randint
diceroll = randint(1,6)
print("You rolled a", diceroll)
Type this program in your development environment.
Run it a few times to see that a random dice roll is displayed each time.
Activity 5
diceroll = 3
if diceroll > 3:
print(diceroll, "is a large roll") else:
print(diceroll, "is a small roll")
print("Yet you rolled a", diceroll)
True or false? .
Whenever this program is executed, it will produce the same output.
True
Subtle points
True or false? .
The instruction highlighted will always be executed.
True
What will this output be?
Activity 5
from random import randint
diceroll = randint(1,6)
if diceroll > 3:
print(diceroll, "is a large roll") else:
print(diceroll, "is a small roll")
print("Yet you rolled a", diceroll)
True or false? .
Whenever this program is executed, it will produce the same output.
False
Subtle points
True or false? .
The instruction highlighted will always be executed.
False
Activity 5
from random import randint
coin = randint(0,1)
if coin == 0:
result = "heads"
print(result)
else:
result = "tails"
Subtle points
Question .
When this program is executed, what will its output be?
1
2
3
▹
4
Summary
In this lesson, you..
Next lesson, you will...
Explore how selection can handle more than two possible branches
Introduce iteration (while statements) to allow the flow of program execution to include loops
Used selection (if statements) to control the flow of program execution
Introduced elements of randomness into your programs