MCQ: python while-loops

Feb 13, 2014 • Isabel Fenton

Question to distinguish novices from competent practitioners before you start teaching:

What do while-loops do?

1) Repeat a chunk of code a given number of times
2) Repeat a chunk of code until a condition is true
3) Repeat a chunk of code until a condition is false
4) Repeat a chunk of code indefinitely

 

One multiple-choice question to tell whether your audience has actually learned what you set out to teach

Which of these loops will not finish?

1) i = 1
  while i <= 10:
    j = i * 3
    print j
    i = i + 2
 

2) i = 1
  while i != 20:
    j = i * 5
    print j
    i = i + 2
 

3) i = 1
  while True:
    j = i * 7
    print j
    break
    i = i + 2
 

4) i = 1
  while True:
    j = i * 10
    print j
    i = i + 2
    if j == 20:
      break
 

One short practical exercise they could do to exercise their new knowledge

Fill in the code as a while loop to print out the first 12 numbers in the 10 times table

while :
  j = i * 10
  print j