l4 Himework
l4 Himework
l4 Himework
Homework
Task 1 .
Read the Python programs below. They are almost identical, except for the fact that the program on
the left uses consecutive if statements, whereas the program on the right uses a single if, elif
statement.
Question 1
When the program on the left is executed, what will be displayed on the screen?
A. small
B. medium
C. large
D. medium
It will print small
Question 2
When the program on the right is executed, what will be displayed on the screen?
A. small
B. medium
C. large
D. medium
It will print medium
Page 1
Year 8 – Intro to Python programming Homework
Lesson 4 – More branches
Task 2 .
Rearrange the lines in the Python program below, so that you have a runnable program that
simulates the rolling of two dice and displays a message that informs the user of the ‘value’ of the
roll.
If the two dice reveal different faces, the value of the roll is the sum of the faces. Otherwise, the
value of the roll is four times the (common) face.
1 if dice1 == dice2:
2 print(value)
3 dice1 = randint(1,6)
4 from random import randint
5 value = dice1 + dice2
6 value = 4 * dice1
7 dice2 = randint(1,6)
8 else:
Write your rearranged program below. You may also indent any of the lines, if you think that it is
necessary.
dice 1=randint(1,6)
dice2=randint(1,6
from random import randint
If dice1==dice2
Value=4*dice1
Else:
Value=dice1+dice2
Print(value)
Page 2
Year 8 – Intro to Python programming Homework
Lesson 4 – More branches
Task 3 .
The incomplete program below is supposed to read two numbers that the user types on the
keyboard and display the value of the largest one, or a message if they are equal.
Step 1
Fill in the conditions in lines 4, 6, and 8 so that the program behaves as specified.
Step 2
Is it really necessary to check the condition in line 8 or could the elif statement be replaced with an
else statement? Justify your answer.
Page 3
Year 8 – Intro to Python programming Homework
Lesson 4 – More branches
This resource is licensed under the Open Government Licence, version 3. For more information on this licence, see
ncce.io/ogl.
Page 4