Edexcel GCSE Computer Science (1CP2) · Problem solving with programming
Practise Sequence, selection and iteration. 29 exam-style questions plus unlimited generated ones on this subtopic, at up to four difficulty levels, with full mark schemes and a progress tracker. Free, no account needed.
Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.
Look at this program.
01 total = 0
02 count = 0
03 number = int(input("Enter a number (0 to stop): "))
04 while number != 0:
05 total = total + number
06 count = count + 1
07 number = int(input("Enter a number (0 to stop): "))
08 if count > 0:
09 print("Mean:", total / count)
10 else:
11 print("No numbers entered")
Give the line number where repetition starts.
04
State the Python keyword that starts a condition-controlled loop.
while
A program should print the square of each number from 1 to 5, and whether each square is even or odd. The program below contains two errors.
01 # Square number table
02 print("Square numbers")
03 for i in range(1, 5):
04 square = i * i
05 print(i, "squared is", square)
06 if square % 2 = 0:
07 print("Even")
08 else:
09 print("Odd")
10 print("Finished")
Identify the type of error on line 06.
Syntax error (= is used instead of ==).
Sample questions
Written for this site in the style of Edexcel exam questions. They are not taken from real past papers.
Question 1Easy5 marks
Look at this program.
01 total = 0
02 count = 0
03 number = int(input("Enter a number (0 to stop): "))
04 while number != 0:
05 total = total + number
06 count = count + 1
07 number = int(input("Enter a number (0 to stop): "))
08 if count > 0:
09 print("Mean:", total / count)
10 else:
11 print("No numbers entered")
(a) Give the line number where repetition starts.[1]
(b) Give the line number where selection starts.[1]
(c) State whether the loop is count-controlled or condition-controlled.[1]
(d) The user enters 6, 10, 5 and 0. State the output.[1]
(e) Define the term sequence in a program.[1]
Show the answer and mark scheme
(a)Answer: 04
04
(b)Answer: 08
08
(c)Answer: Condition-controlled
condition-controlled
(d)Answer:Mean: 7.0
Mean: 7.0 (accept 7)
(e)Answer: The instructions are carried out one after another, in the order in which they are written.
instructions are carried out one after another / in the order they are written
Question 2Medium8 marks
Here are two programs. Program A
for n in range(3, 20, 4):
print(n * 2)
Program B
total = 0
number = 1
while total < 50:
total = total + number
number = number * 2
print(number)
(a) State the numbers output by Program A.[2]
(b) Write a program that uses a while loop to produce exactly the same output as Program A.[3]
(c) State the output of Program B.[1]
(d) Explain why a while loop is used in Program B rather than a for loop.[2]
Show the answer and mark scheme
(a)Answer: 6, 14, 22, 30, 38
6, 14 and 22
30 and 38, with no other numbers
(b)Answer:
n = 3
while n < 20:
print(n * 2)
n = n + 4
n set to 3 before the loop
while n < 20: (accept n <= 19)
n increased by 4 inside the loop, after the print statement
(c)Answer: 64
64
(d)Answer: The number of passes is not known in advance, because it depends on when the total reaches 50. A while loop repeats until a condition is met.
the number of passes / repetitions is not known in advance (it depends on when total reaches 50)
a while loop is condition-controlled / repeats while a condition is True
Question 3Hard8 marks
A primary school wants a program to help pupils practise their times tables.
Write a program that:
asks the user 10 multiplication questions
makes each question by choosing two random whole numbers from 2 to 12
inputs the user's answer to each question and outputs whether it is correct; if it is wrong, the correct answer is also output
outputs the number of correct answers at the end, followed by Well done if the user got 8 or more correct.
[8]
Show the answer and mark scheme
Answer:
import random
score = 0
for question in range(10):
a = random.randint(2, 12)
b = random.randint(2, 12)
answer = int(input("What is " + str(a) + " x " + str(b) + "? "))
if answer == a * b:
print("Correct")
score = score + 1
else:
print("Wrong, the answer is", a * b)
print("You scored", score, "out of 10")
if score >= 8:
print("Well done")
imports the random library and uses random.randint(2, 12)
a loop that repeats exactly 10 times
two new random numbers are chosen inside the loop, for every question
displays each question, including both numbers
inputs the answer and converts it to an integer
compares the answer with the product of the two numbers
outputs a message for a correct answer, and outputs the correct answer when the answer is wrong
a score is set to 0 before the loop and increased by 1 for each correct answer
after the loop, outputs the score, and outputs Well done only when the score is 8 or more