Exam practice
Every question from the end of every unit, with hints and answers to open. After you check an answer, mark how you did, and come back to the ones you didn’t get.
10 questions
Unit 2 · Decisions and Loops in Python
What is the difference between if … elif … else and several separate ifs? Give an example where they print different things.
Answer
With if … elif … else, Python asks the conditions in order and runs only the block of the first one that is true: exactly one path is taken (or none, without an else). Separate ifs are separate questions: each is asked, and any number of them can run.
For n = 10:
if n > 5: print("big")elif n > 0: print("positive")prints only big, but
if n > 5: print("big")if n > 0: print("positive")prints big and then positive.
Write the numbers each of these gives: i. range(6) ii. range(3, 8) iii. range(1, 10, 3) iv. range(5, 0, -1) v. range(4, 4)
Answer
- i.0, 1, 2, 3, 4, 5
- ii.3, 4, 5, 6, 7
- iii.1, 4, 7
- iv.5, 4, 3, 2, 1
- v.no numbers at all: it starts at 4 and must stop before 4. A loop over it runs 0 times.
Write the output of this program.
for i in range(1, 4): print(i, i * i)print("Done")Answer
Output
1 12 43 9Donei is 1, 2 and 3, and each time round the program prints it and its square. Done is not indented, so it is printed once, after the loop.
Make a trace table for this program, and give its output.
a = 1b = 0while a < 20: b = b + 1 a = a * 3print(a, b)Answer
| Round | a < 20 | b | a |
|---|---|---|---|
| start | 0 | 1 | |
| 1 | True | 1 | 3 |
| 2 | True | 2 | 9 |
| 3 | True | 3 | 27 |
| False (27 < 20) |
Output
27 3a is multiplied by 3 each round, and b counts the rounds. After three rounds a is 27, the condition is False, and the loop ends.
Write a program that prints the 5 times table from 5 × 1 to 5 × 12, then the total of all twelve answers.
Answer
total = 0for i in range(1, 13): answer = 5 * i print("5 ×", i, "=", answer) total = total + answerprint("Total:", total)Output
5 × 1 = 55 × 2 = 105 × 3 = 155 × 4 = 205 × 5 = 255 × 6 = 305 × 7 = 355 × 8 = 405 × 9 = 455 × 10 = 505 × 11 = 555 × 12 = 60Total: 390range(1, 13) gives 1 to 12. total is a running total, set to 0 before the loop and added to inside it.
Write a Python program that makes the turtle draw an equilateral triangle with sides of 100 steps, using a loop. Then say what you would change to draw a regular pentagon.
Answer
import turtlet = turtle.Turtle()for i in range(3): t.forward(100) t.right(120)turtle.done()Each turn is 360° ÷ 3 = 120°. For a pentagon, loop 5 times and turn 360 ÷ 5 = 72° each time: range(5) and t.right(72).
A student wrote this to say whether a temperature is hot (over 30 °C), warm (16 to 30) or cold (15 and below). It says Warm for 35 °C. Explain the mistake and correct it.
temp = 35if temp > 15: print("Warm")elif temp > 30: print("Hot")else: print("Cold")Answer
The conditions are in the wrong order. 35 > 15 is True, so the first block runs and prints Warm, and the elif temp > 30 is never asked. Ask the biggest first:
temp = 35if temp > 30: print("Hot")elif temp > 15: print("Warm")else: print("Cold")Now 35 prints Hot, 20 Warm and 10 Cold. Test the edges too: 30 is Warm, 31 Hot, 15 Cold, 16 Warm.
This program should print the even numbers from 2 to 10, but it never stops. Why? Correct it in two different ways.
n = 2while n != 11: print(n) n = n + 2Answer
n goes 2, 4, 6, 8, 10, 12, 14… It is always even, so it is never 11, and n != 11 is always True: an infinite loop.
Way 1: while n <= 10: stops as soon as n passes 10.
Way 2: a for loop, which can’t go on forever: for n in range(2, 11, 2): print(n).
Explain the role of each variable, each loop and each if in this program, and say what it prints when the user types 4, 7 and then 0.
count = 0total = 0number = int(input("A number (0 to stop): "))while number != 0: count = count + 1 total = total + number number = int(input("A number (0 to stop): "))print(count, "numbers, adding up to", total)Answer
- count is a counter: it starts at 0 and goes up by 1 for each number typed (not counting the 0).
- total is a running total: it starts at 0 and each number is added to it.
- number is the input value: the latest number typed. It is asked once before the loop, so the condition has something to check, and again at the end of each round.
- The while loop (iteration) repeats while the number typed isn’t 0: the user decides how many times, so it can’t be a for loop. The 0 is a signal to stop, not a number to add.
- There is no if: the while’s condition is the only choice the program makes.
With 4, 7, 0 the loop runs twice: count becomes 1, then 2, and total 4, then 11. At the 0 the condition is False and the loop ends.
Output
A number (0 to stop): 4A number (0 to stop): 7A number (0 to stop): 02 numbers, adding up to 11A shopkeeper has 50 kg of rice and sells 7 kg a day. Write a program with a while loop that works out how many full days the rice lasts, and how much is left. Then say how you could get the same answers without a loop, using Unit 1’s operators.
Answer
rice = 50days = 0while rice >= 7: rice = rice - 7 days = days + 1print("Full days:", days)print("Left over:", rice, "kg")Output
Full days: 7Left over: 1 kgWithout a loop: 50 // 7 is 7 days and 50 % 7 is 1 kg. Taking 7 away again and again until you can’t is exactly what integer division and the remainder mean.
