Algorithms and Python (1)
Answers are for parents and teachers.
1
What does this program print? Trace the variable first.
Python
1total = 0 2for i in range(1, 4): 3 total = total + i * 2 4print(total)
12. total goes 0, then 2, 6, 12 (adds i * 2 for i = 1, 2, 3).
2
A program has x = 10 and prints x / 4, x // 4 and x % 4. Write the three outputs.
2.5, 2 and 2.
3
Write a Python program that prints the numbers 5, 4, 3, 2, 1 with a while loop.
n = 5; while n > 0: print(n); n = n - 1 (with print and n = n - 1 indented inside the loop).
4
Spot the mistake in this program and write the corrected line: age = 15, then if age = 18: print("adult").
In the condition we compare, so we need ==: if age == 18:. A single = would store a value and is not allowed there.
5
Draw the flowchart of a program that reads a mark and shows “pass” if the mark is at least 50, otherwise “fail”. Which structure is it?
Start, input mark, a diamond “mark ≥ 50?”, yes: show pass, no: show fail, end. It is branching.