Remainders, decisions and averages
Answers are for parents and teachers.
1
Find 22 % 5 and 30 % 5. What do the two remainders say about 22 and 30?
22 % 5 is 2 and 30 % 5 is 0. 30 fits exactly into groups of 5; 22 leaves 2 over.
2
A friend writes if num % 2 = 0: in a program. What is wrong, and how do you fix it?
A single = stores a value; the test needs two equal signs: if num % 2 == 0:
3
The user types 9 in the even-or-odd program. What does the screen show, and what would a last unindented print show?
Python
1num = int(input("Enter a number: ")) 2if num % 2 == 0: 3 print("Even number") 4else: 5 print("Odd number") 6print("Done")
Odd number. The unindented last line runs for every input, so it is always shown after the branch.
4
Marks 40, 70 and 100: write the line for the average and say what it shows. What would a + b + c / 3 show?
avg = (a + b + c) / 3 shows 70.0. Without brackets: 143.33333333333334.
5
Give one reason why / gives 60.0 and not 60, and one use of //.
The sign / always gives a decimal result. The sign // is useful when only the whole-number part is wanted, such as 7 // 2 = 3.