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