1 of 26

Lesson 3:

At a crossroads

Year 8 – Intro to Python programming

2 of 26

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?

3 of 26

Starter activity

Selection

When your programs check conditions and select the path of action that they will follow accordingly

4 of 26

Objectives

  • Use selection (if statements) to control the flow of program execution between branches
  • Introduce elements of randomness into your programs

In this lesson, you will...

5 of 26

Activity 1

Selection

if :

else:

condition

block of statements

block of statements

if :

block of statements

condition

6 of 26

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

7 of 26

Activity 1

Selection in Python: Greeting

Use pair programming .

Driver

Control the keyboard and mouse.

Navigator

Provide support and instructions.

Alternate between roles.

8 of 26

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)

9 of 26

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.

10 of 26

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.

11 of 26

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

12 of 26

Activity 2

Practise using selection

Extend some of the programs that we’ve built so far to use selection.

Use your worksheet.

13 of 26

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

14 of 26

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

15 of 26

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 .

16 of 26

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

17 of 26

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

18 of 26

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)

19 of 26

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.

20 of 26

Activity 4

Question .

You are making a dice rolling game. Fill in the gap to complete the program.

  1. int(input())
  2. 1 to 6
  3. pick random (1) to (6)
  4. randint(1,6)

1

2

3

4

Randomness

from random import randint

diceroll =

print("You rolled a", diceroll)

21 of 26

Activity 4

Question .

When this program is executed, what will its output be?

  1. You rolled a diceroll
  2. You rolled a randint(1,6)
  3. You rolled a and a random integer ranging from 1 to 6
  4. It is not possible to know the output without executing the program

1

2

3

4

Randomness

from random import randint

diceroll = randint(1,6)

print("You rolled a", diceroll)

22 of 26

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.

23 of 26

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?

24 of 26

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

25 of 26

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. Either 0 or 1
  2. Either heads or nothing
  3. Either heads or tails
  4. heads

1

2

3

4

26 of 26

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