09 - While Loops

 1. Iteration refers to the use of __________ in programming
Note: The following video provdies a beginner's overview to While loops. Do watch it if possible.

  lexical analysis



  lossy compression

 2. There are three main types of loops. Which of the following is NOT a type of loop
For, While, Repeat..Until, Print, Def

  There is no such thing as a 'for' loop

  Print and Def are not types of loops


  All of the items on the list are valid loops

 3. A loop, by definition, is getting code to ___________ itself and every loop needs a __________ condition.

  function / starting

  subtract / final

  repeat / stopping

  define / variable

 4. A while loop is different from a repeat…until loop in that …
Note: Repeat …Until loops exist in languages like VB.Net but are not called this in Python. 

  A while loop has its stopping condition at the start

  A while loop has no stopping condition

  All the options listed here apply

  A while loop has its stopping condition at the end

 5. In a language like VB.Net a while loop must end with an 'End While' statement, and it is the same in python



 6. Analyse the following code - what will the output be?
def main():
    while i<10:



  An infinite loops of 0s (zeros)



 7. Analyse the code below and see if you can fix the loop to produce an output of 0.1.2,3,4,5,6,7,8,9
def main():
    i=0 #this is the value of i to start with (initial value)
    while i<10: #this is the condition of the loop
        #i will be printed because the condition is true
        print(i)#i is still 0, and it will be printed forever!
        #What can we add here to make it print 0,1,2,3,4,5,6,7,8,9?

  None of the above

  replace the last comment with i+0 (this will add 0 to the value of i)

  simple replace the last comment with i=i+1 (this will increase the value of i until it reaches 10)

  replace the last comment with i+9 (this will add 9 to the value of i)

 8. The following while loop produces an output of 2,4,6,8. How can you make it go up to 10?
def main():
    while i<10:


  Change the stopping condition to: while i<12:

  Change the stopping condition to: while i=12

  Change the stopping condition to: while i>12:

  Change the stopping condition to: while < 12 MOD 3

 9. Which of the following options produces an output of 10,9,8,7,6,5,4,3,2,1
#Option 1
def main():
    while i<11:

#Option 2
def main():
    while i>0:

#Option 3
def main():
    while i>10:

  Option 1

  Option 2

  None of the above

  Option 3

 10. Run the following code, or trace its logic. Why will it never accept the password 'open123'?
def main():
    while password_correct==False:
        password=input("Please enter your password:")
        if password=="open123":

    print("Password correct - exit the loop")

  None of the above

  Because the IF statement inside the while loop has been set up incorrectly (it shoud have a single equals sign)

  Because the condition to exit the loop is never met (change password_correct=False on line 6 to True)

  Because open123 is not the set password (numbers are not permitted)

 11. What is happening in the following program? Which statement describes it best?
def gameplay():
    while score<11:
    print("Exit Game")

  None of the above

  The score starts at 8, the loop outputs 8,9,10 and then exits the loop to print "Exit game"

  The score starts at 0, as always, the loop outputs 0 to 10, and then exits the game

  Thes score starts at 8 and inside the loop the output is 8,9,10,11 before there is an error

 12. What change needs to be done to the following code to make the output just '5'
while x<4:

  Both changing line 2 to: while x==5 and changing line 2 to: while x<6: will work to produce an output of 5

  Change line 2 to: while x =5:

  Change line 2 to: while x<6:

  Change line 2 to: while x==5:

 13. The use of a while loop in a program means that …

  even if the condition isn't met, the code in the loop will run at least once

  unless the condition is met, the code inside the loop may not run at all

  None of the above

  when the condition is met, the while loop will delete itself

 14. How could a while loop be used in game design? Which statement is correct?

  A while loop could be used to print output directly to the printer

  A while loop could be used to import and generate random numbers

  A while loop could be used to replace a function, as while loops are functions

  While the health of a character is greater than 0, then continue gameplay

 15. A while loop is different from a 'for' loop in that ….

  All of the above apply

  In a for loop the stopping condition is always sandwiched in the middle

  In a for loop the condition to stop the loop is at the end

  In a for loop, the number of iterations are known and declared in advanced (e.g. from 0 to 10)