Python · 2.1
Loops: for and while
Name
Class
Date
Mark / 29
Section A
Circle the letter of the correct answer. One mark each.
- 1How many times does
for i in range(6)repeat?1- A0
- B5
- C6
- D7
- 2What does
range(2, 8)produce?1- A2 to 8 including 8
- B8, 7, 6, 5, 4, 3, 2
- C2, 4, 6, 8
- D2, 3, 4, 5, 6, 7
- 3Which loop should be used when the number of repetitions is unknown?1
- AA while loop
- BA for loop
- CNo loop
- DA nested for loop
- 4What causes an infinite loop?1
- AThe counter starts at 0
- BThe loop contains an if
- CThe loop uses range()
- DNothing inside the loop changes the value the condition tests
- 5What does
breakdo?1- ALeaves the loop immediately
- BSkips to the next iteration
- CRestarts the loop
- DEnds the program
- 6In a nested loop where the outer runs 5 times and the inner runs 3, how many times does the inner body run?1
- A3
- B5
- C8
- D15
- 7Why should a maximum be initialised to the first element rather than 0?1
- AIf all values are negative, nothing is greater than 0 and the answer is wrong
- B0 is not a valid number
- CIt runs faster
- DThe list might be empty
- 8What does
continuedo?1- ARepeats the current iteration
- BEnds the program
- CSkips the rest of the current iteration and starts the next
- DLeaves the loop
- 9What does
for letter in "cat"loop over?1- AThe whole word once
- BEach character of the string in turn
- CThe length of the string
- DNothing
- 10What does
enumerate()give you?1- AThe length of the list
- BBoth the index and the item on each iteration
- COnly the index
- DOnly the item
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 and give one situation for each.4
- 2WriteWrite a program that keeps asking the user for numbers until they enter -1, then prints how many numbers were entered and their total.5
- 3ExplainExplain what causes an infinite loop and how to prevent one.3
- 4StateA program has a loop inside another loop. The outer loop runs 4 times and the inner loop runs 6 times. State how many times the inner loop body executes in total and explain why.3
- 5WriteWrite a program that finds and prints the largest number in the list [12, -5, 33, 8, -47, 21] without using the max function.4
Mark scheme
Section A
Section B
1 State the difference between a for loop and a while loop and give one situation for each. 4
One mark for each of the following, to a maximum of 4.
- A for loop repeats a known number of times or over a collection
- Situation such as processing every item in a list
- A while loop repeats while a condition is true
- Situation such as validating input until it is correct
Model answer. A for loop repeats a known number of times, or once for each item in a collection, so the number of iterations is decided before the loop begins. It would be used to work through every mark in a list of 30 students, because the number of repetitions is known from the length of the list. A while loop repeats for as long as its condition remains true, so the number of iterations is not known in advance and depends on what happens while the program is running. It would be used to keep asking a user to enter a valid password until they get it right, since there is no way to know how many attempts they will need.
2 Write a program that keeps asking the user for numbers until they enter -1, then prints how many numbers were entered and their total. 5
One mark for each of the following, to a maximum of 5.
- Initialises a total and a counter
- Uses a while loop
- Reads and converts input inside the loop
- Stops when -1 is entered without counting it
- Prints the count and total after the loop
Model answer. total = 0 count = 0 number = int(input("Enter a number, -1 to finish: ")) while number != -1: total += number count += 1 number = int(input("Enter a number, -1 to finish: ")) print("You entered", count, "numbers") print("Their total is", total) The first number is read before the loop so the condition has something to test. The loop continues while the number is not -1, adding to the total and increasing the counter each time before reading the next value. Because the read happens at the end of the loop body, the -1 that ends the loop is never counted or added.
3 Explain what causes an infinite loop and how to prevent one. 3
One mark for each of the following, to a maximum of 3.
- The condition never becomes false
- Because nothing inside the loop changes the value being tested
- Prevent it by ensuring the loop body updates the variable in the condition
Model answer. An infinite loop happens when the condition a while loop tests never becomes false, so the loop has no way to end. This is almost always because nothing inside the loop body changes the value that the condition depends on, for example writing while count is less than 10 but never increasing count. It can also happen when the change moves the value in the wrong direction. To prevent it, always check that the loop body contains a statement that alters the variable in the condition, and that it alters it in a direction that will eventually make the condition false.
4 A program has a loop inside another loop. The outer loop runs 4 times and the inner loop runs 6 times. State how many times the inner loop body executes in total and explain why. 3
One mark for each of the following, to a maximum of 3.
- The inner body runs 24 times
- The inner loop completes fully for each outer iteration
- So the totals are multiplied together
Model answer. The inner loop body executes 24 times in total. Each time the outer loop runs one iteration, the inner loop starts from the beginning and completes all six of its own iterations before control returns to the outer loop. Since the outer loop repeats four times, the inner body runs six times on each of those four occasions, and 4 multiplied by 6 gives 24. This multiplication is why nested loops become expensive very quickly on large amounts of data.
5 Write a program that finds and prints the largest number in the list [12, -5, 33, 8, -47, 21] without using the max function. 4
One mark for each of the following, to a maximum of 4.
- Initialises the largest to the first element of the list
- Loops through every item in the list
- Compares each item with the current largest
- Updates the largest when a bigger value is found and prints the result
Model answer. numbers = [12, -5, 33, 8, -47, 21] largest = numbers[0] for n in numbers: if n > largest: largest = n print("The largest number is", largest) The variable largest is initialised to the first element of the list rather than to 0, which matters because the list contains negative numbers and initialising to 0 would give a wrong answer if every value were negative. The loop then compares each value with the current largest and replaces it whenever a bigger one is found, so after the loop finishes largest holds the maximum.