Key Stage 3 · Y8.3
Python: Loops, Lists and Functions
Name
Class
Date
Mark / 27
Section A
Circle the letter of the correct answer. One mark each.
- 1How many times does
for i in range(4)repeat?1- A0
- B3
- C4
- D5
- 2What is
scores[0]ifscores = [10, 20, 30]?1- A0
- B10
- C20
- D30
- 3Which loop should be used to keep asking until the user types a valid answer?1
- AA for loop
- BA nested for loop
- CNo loop is needed
- DA while loop
- 4What does
scores.append(50)do?1- ARemoves 50 from the list
- BAdds 50 to the end of the list
- CReplaces the first item with 50
- DSorts the list
- 5What does the
returnkeyword do in a function?1- ASends a result back to the code that called the function
- BEnds the program
- CStarts a loop
- DPrints a value on the screen
- 6What is wrong with
while x < 10: print(x)if x is 0?1- Aprint cannot be used inside a while loop
- BNothing is wrong
- Cx should be a string
- Dx never changes, so it is an infinite loop
- 7How would you find the highest value in a list called marks?1
- Amax(marks)
- Bhigh(marks)
- Cmarks.top()
- Dsort(marks)
- 8A list has 7 items. What is the index of the last item?1
- A0
- B6
- C7
- D8
- 9What is validation?1
- AChecking the spelling of variable names
- BMaking the program run faster
- CChecking that input is sensible before the program uses it
- DSaving data to a file
- 10Why are functions useful?1
- ACode can be written once and used many times, so it is easier to fix
- BThey make the program run at a higher clock speed
- CThey automatically validate input
- DThey remove the need for variables
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1StateState the difference between a for loop and a while loop.2
- 2WriteWrite a Python program that asks the user for five numbers, stores them in a list, and then prints the total and the average.5
- 3ExplainExplain what validation is and describe one validation check that could be used when asking for a percentage mark.3
- 4ExplainExplain two reasons why a programmer would use functions in a program.4
- 5ExplainA program uses
while count < 10:and prints count inside the loop, but the program never stops. Explain why and how to fix it.3
Mark scheme
Section A
Section B
1 State the difference between a for loop and a while loop. 2
One mark for each of the following, to a maximum of 2.
- A for loop repeats a known number of times
- A while loop repeats while a condition is true, and the number of repetitions may not be known
Model answer. A for loop repeats a known, fixed number of times, or once for every item in a collection such as a list. A while loop repeats for as long as a condition remains true, so the number of repetitions is not necessarily known in advance and depends on what happens while the program runs.
2 Write a Python program that asks the user for five numbers, stores them in a list, and then prints the total and the average. 5
One mark for each of the following, to a maximum of 5.
- Creates an empty list
- Uses a for loop to repeat five times
- Takes input and converts it to a number
- Adds each number to the list
- Calculates and prints the total and average
Model answer. numbers = [] for i in range(5): value = int(input("Enter a number: ")) numbers.append(value) total = sum(numbers) average = total / len(numbers) print("Total:", total) print("Average:", average) An empty list is created first. The for loop runs five times, and each time it reads a number, converts it from the text returned by input into an integer, and appends it to the list. After the loop the built in sum function adds up the list and dividing by len gives the average.
3 Explain what validation is and describe one validation check that could be used when asking for a percentage mark. 3
One mark for each of the following, to a maximum of 3.
- Validation checks that input is sensible before it is used
- Names a suitable check such as a range check
- Explains the check, such as ensuring the value is between 0 and 100
Model answer. Validation means checking that the data a user has entered is sensible and in the expected form before the program tries to use it, so that the program does not crash or produce meaningless results. For a percentage mark, a range check would be used to confirm that the value entered is between 0 and 100 inclusive, since a mark of 150 or -20 is impossible. A type check would also be needed to confirm that a whole number has been entered rather than letters, because trying to convert text such as hello into a number would cause the program to crash.
4 Explain two reasons why a programmer would use functions in a program. 4
One mark for each of the following, to a maximum of 4.
- Code can be written once and called many times
- This avoids duplication so the program is shorter
- If a change is needed it only has to be made in one place
- Each function can be tested on its own, making errors easier to find
Model answer. The first reason is that a function lets code be written once and then used as many times as needed, simply by calling its name. This avoids the same lines being copied out repeatedly, so the program is much shorter and easier to read, and crucially it means that if the code ever needs correcting or changing, the edit is made in one place rather than being repeated everywhere the code appears. The second reason is that functions make a program much easier to develop and test. Each function does one clearly defined job, so it can be tested on its own with different inputs until it is known to work, which makes faults far easier to locate than in one long program where everything is mixed together.
5 A program uses while count < 10: and prints count inside the loop, but the program never stops. Explain why and how to fix it. 3
One mark for each of the following, to a maximum of 3.
- The value of count is never changed inside the loop
- So the condition remains true forever and the loop never ends
- Fix by adding a line inside the loop that increases count
Model answer. The loop never stops because nothing inside it changes the value of count. The condition count less than 10 is tested before every repetition, but since count keeps the same value it stays true forever, producing an infinite loop that prints the same number endlessly. The fix is to add a line inside the loop that increases count each time, such as count = count + 1, so that count rises towards 10 and the condition eventually becomes false, allowing the loop to end.