l4 Himework

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Year 8 – Intro to Python programming Homework

Lesson 4 – More branches

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.

1 number = 13: number = 13:


2 if number < 10: if number < 10:
3 print("small") print("small")
4 if number < 100: elif number < 100:
5 print("medium") print("medium")
6 if number < 1000: elif number < 1000:
7 print("large") print("large")

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.

1 print("Enter two integers:")


2 num1 = int(input())
3 num2 = int(input())
4 if num1>num2:
5 print("The largest of the two is", num1)
6 elif num2>num1:
7 print("The largest of the two is", num2)
8 elif num2=num1:
9 print("These numbers 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy