Preview

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.

  lops

  lossy compression

  lexical analysis

  loops

 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

  All of the items on the list are valid loops

  Repeat…Until

  Print and Def are not types of loops

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

  define / variable

  repeat / stopping

  function / starting

  subtract / final

 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 no stopping condition

  All the options listed here apply

  A while loop has its stopping condition at the start

  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

  TRUE

  FALSE

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

main()

  0

  1,2,3,4,5,6,7,8,9,10

  0,1,2,3

  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?
main()

  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)

  None of the above

  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():
    i=2
    while i<10:
        print(i)
        i=i+2

main()

  Change the stopping condition to: while i>12:

  Change the stopping condition to: while < 12 MOD 3

  Change the stopping condition to: while i<12:

  Change the stopping condition to: while i=12

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


#Option 2
==========
def main():
    i=10
    while i>0:
        print(i)
        i=i-1
main()



#Option 3
==========
def main():
    i=1
    while i>10:
        print(i)
        i=10-1
main()

  Option 2

  Option 1

  Option 3

  None of the above

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


    print("Password correct - exit the loop")
    
main()

  None of the above

  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)

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

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

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

  None of the above

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

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

 12. What change needs to be done to the following code to make the output just '5'
x=5
while x<4:
    print(x)
    x=x+1

  Change line 2 to: while x<6:

  Change line 2 to: while x==5:

  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:

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

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

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

  None of the above

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

 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

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

  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

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

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

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

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

  All of the above apply