AQA GCSE Computer Science sample Paper 1, Question 15: odd numbers, up or down
AQA 8525 sample assessment material, Paper 1 Question 15: a while loop prints the odd numbers up to an entered odd number, but runs for ever on a negative one. Change the loop body so that it counts down instead. The four marks explained, with the program to run.
Question 15 of AQA's sample assessment material for GCSE Computer Science Paper 1 (8525/1B, the Python paper) is a 4 mark refining question: a loop that works for some inputs and not others, and you change only its body.
We do not copy the paper here. Open it beside this page: AQA 8525 Paper 1B sample question paper (PDF). When you have finished, check the sample mark scheme too.
The question in short
odd starts at 1. The user enters an integer. While odd is not equal to the number, the program prints odd and adds 2. Then it prints Finished.
For 7 it prints 1, 3, 5. For -7 it should print 1, -1, -3, -5, but instead odd climbs 1, 3, 5, 7 ... and never reaches -7. The loop runs for ever.
Change the code inside the loop so that it works for any odd integer.
The fix
If the number is negative, count down instead of up.
odd = 1
number = int(input("Enter an integer: "))
while odd != number:
print(odd)
if number < 0:
odd = odd - 2
else:
odd = odd + 2
print("Finished!")
Where the four marks are
Selection with an else (or two ifs); a correct condition; subtracting 2 under the right condition; and odd printed with exactly one of adding or subtracting on each pass. Any error caps you at 3.
Where the marks are lost
if number != 0. The mark scheme shows this as the 3 mark answer. It is not the right test: for 7 it would count down and never stop.- Changing the
whileline. The question says inside the loop. The conditionodd != numberis already right. - Adding and subtracting on the same pass. Two
ifs without anelsecan both run. if odd > number. It works, and it is accepted. But think it through: it must give the same answer on every pass, or the loop bounces.
Run it
Enter 7, then -7. The robot drives forward for positive numbers and backward for negative.
The program
from bugbot import *
connect()
odd = 1
number = int(input("Enter an odd integer: "))
passes = 0
while odd != number and passes < 20:
print(odd)
passes = passes + 1
if number < 0:
odd = odd - 2
backward(60, distance=3)
else:
odd = odd + 2
forward(60, distance=3)
if passes == 20:
print("That was not an odd number. Stopped.")
print("Finished!")
Questions
What is the answer to AQA sample Paper 1 Question 15?
Inside the loop, after the print, add if number < 0 then odd = odd - 2 else odd = odd + 2.
Why does the original program loop for ever on -7?
odd only ever goes up, so it can never equal a negative number, and the condition odd != number stays true.
What happens if an even number is entered?
odd is always odd, so it can never equal an even number and the loop never ends. The question only asks for odd integers. Validation would fix it.
More from this paper
Every AQA 8525 question we have worked
Learn it step by step
- F6.4 Debugging logic errors Robust programs
- F2.6 Condition-controlled loops: while Decisions and loops
This is our own explanation of a published exam question. It is not written or endorsed by AQA, and the question paper and mark scheme remain AQA's copyright. Read them on AQA's site with the links on this page.