Practice: while

Tags

while practice, and an exercise.

Practice

Multiple choice

What does this output? Answer without running the code.

If you can't work it out, then run it. Remember, you can use the Variable Explorer to watch a program run.

  • counter = 0
  • output = ''
  • while counter < 3:
  •     output += 'doggos '
  •     counter += 1
  • print(output)
Saving
A

Nothing

B
doggos doggos
C
doggos doggos doggos
D
doggos doggos doggos doggos

Not graded. So why do it?

Multiple choice

What does this output with an input of 2? Answer without running the code.

If you can't work it out, then run it. Remember, you can use the Variable Explorer to watch a program run.

  • times = float(input('How many times? '))
  • count = 0
  • output = ''
  • while count <= times:
  •     output += 'Snorlax rox! '
  •     count += 1
  • print(output)
Saving
A

Nothing

B
Snorlax rox! Snorlax rox!
C
Snorlax rox! Snorlax rox! Snorlax rox!
D
Snorlax rox! Snorlax rox! Snorlax rox! Snorlax rox!

Not graded. So why do it?

Multiple choice

What does this output, with an input of 4? Answer without running the code.

If you can't work it out, then run it. Remember, you can use the Variable Explorer to watch a program run.

  • output = 'Ready, set'
  • how_many = float(input('How many? '))
  • while how_many > 0:
  •     how_many -= 1
  •     output += '.'
  • output += ' GO!'
  • print(output)
Saving
A

Nothing

B
Ready, set.. GO!
C
Ready, set... GO!
D
Ready, set.... GO!
E
Ready, set..... GO!

Not graded. So why do it?

Multiple choice

What does this output, with an input of 20? Answer without running the code.

If you need to, lookup what *= means.

If you can't work it out, then run it. Remember, you can use the Variable Explorer to watch a program run.

  • value = 1
  • max = float(input("What's the max? "))
  • while value * 2 < max:
  •     value *= 2
  • print(value)
Saving
A

Nothing

B

8

C

16

D

20

E

32

Not graded. So why do it?