Year 8 – Intro to Python programming Homework - Solutions
Lesson 2 – Crunching numbers
Homework – Solutions
Task 1 .
Read the Python program below:
1 num1 = int(input())
2 num2 = 10 + num1 * 2
3 print(num2)
4 num1 = 20
5 print(num1)
Question 1
When this program is executed, if the user types 10 on the keyboard, what will be
displayed on the screen as a result of executing line 3?
A. 30
B. 40
C. 10 + 10 * 2
D. 10 + num1 * 2
The correct answer is A: 30.
Operator precedence is important for the calculation (answer B). Expressions are
evaluated during execution, and it’s their value that is stored in variables (answers C and
D).
Question 2
When this program is executed, if the user types 10 on the keyboard, what will be
displayed on the screen as a result of executing line 5?
A. 10
B. 20
C. 10 and 20
Page 1 Last updated: 16-04-21
D. There is an error in the program because a variable cannot hold two values at the
same time
The correct answer is B: 20.
Line 4 replaces the value of num1 (answer A); it does not assign it an additional value
(answer D). Variables can only hold a single value at any given time (answer C).
Task 2 .
Rearrange the lines in the Python program below, so that you have a runnable program
that holds a meaningful interaction with the user.
1 print("And where do you live", name)
2 print("I’ve never been to", location)
3 name = input()
4 print("What is your name?")
5 location = input()
Write your rearranged program below:
4 print("What is your name?")
3 name = input()
1 print("And where do you live", name)
5 location = input()
2 print("I’ve never been to", location)
Task 3 .
The incomplete program below is supposed to prompt the user for a distance in miles,
and convert the distance that the user enters to kilometres.
1 print("Enter a distance in miles:")
2 miles = float(input()) # int is also correct.
3 kilometres = 1.60934 * miles.
4 print(miles, "miles is", kilometres, "km")
Step 1
Page 2
Complete line 2 so that the value assigned to the miles variable is obtained from what
the user types on the keyboard.
Step 2
Complete line 3 so that the program calculates the value of the kilometres variable to
be the equivalent of the miles variable, converted to kilometres. Note that 1 mile is equal
to 1.60934 kilometres.
Resources are updated regularly — the latest version is available at: ncce.io/tcc.
This resource is licensed under the Open Government Licence, version 3. For more information on this
licence, see ncce.io/ogl.
Page 3