Preview

09 - While Loops

 1. The pseudo code and flowchart show the use of a while loop. Can you fill in the blanks?
uploads/while_loop1.png

  WHILE / counter + counter

  WHILE / END WHILE

  FOR / + 1

  WHILE / counter + 1

 2. If a program needs some data from a user and they keep on typing invalid data, it makes sense to keep on asking them to enter the right stuff until they do. Can you fill in the blanks on line 5?
#Pseudocode below

OUTPUT "Enter an Integer"
Success:=False
WHILE Success= ________ DO
    INPUT possiblyInteger
    IF INT(possiblyInteger) IS VALID THEN
        Success:=True
LOOP

  NOT FALSE

  TRUE

  FALSE

  1

 3. The following code uses an if statement within a while loop. The loop will only start and continue if ...

  counter is smaller than the word (e.g. 0 is smaller than "hello") or contains_y = True

  contains_y = True and counter is more than the length of the entered word

  contains_y = False and counter is less than the length of the entered word

  contains_y is False and counter is less than 0.

 4. The difference between a repeat -until loop and a while loop is that the condition in a while loop is at the _____________. In a for loop however, both the ___________________ is pre-defined at the start.
#While loop
def main():
   score=500 #initialise the score variable to 500
   while score>100: #condition for loop, while score is ABOVE 100
       print("Whoo, you're in a loop") #print this while above condition true
       score=score+1 #the score keeps going up for each loop round

main()


#For loop
def main():
  for i in range(0,100,5):
    print(i)

main()

  start / false, true and increment value

  end / start and stop

  start / start and stop (and increment if applicable)

  end / loop and end

 5. The following program prints "You are rich" ....and keeps doing so! What do you need to change on line 7 to make it print "You are rich" only four times? See comments in code for a hint.

  4 =gold_coins - 10 (value of 4 must be set to gold_coins)

  gold_coins = + 4 (value of gold_coins must be set to 4)

  gold_coins=gold_coins + gold_coins (value of gold_coins must go up to stop)

  gold_coins=gold_coins - 10 (value of gold_coins must go down to meet condition)

 6. The following program will print 0 forever! We want to print the numbers: 0,1,2,3,4,5. What is missing on line 5?
def main():
  x=0
  while x<6:
    print(x)
	#what is missing here?
    
main()

  x = x + 1 (incrementation so that it goes up to meet the stopping condition)

  x = x (set x to equal x so that it stops and doesn't go on forever)

  x = 5 (set x to equal 5 so that it prints 1,2,3,4,5)

  x = x - 1 (decremenation so that it goes down to meet the stopping condition)

 7. What would you need to change on line 2 and 3 in order to print 1,2,3,4,5,6,7,8,9,10
def main():
  x=0
  while x<6:
    print(x)
    x=x+1
    
main()

  Line 2: x=1 Line 3: while x<11:

  Line 2: x=0 (unchanged) Line 3: while x<11:

  Line 2: x=11 Line 3: while x<11:

  Line 2: x=11 Line 3: while x<10:

 8. In the following code a boolean flag (found=False) is used within a while loop. On line 4 if the user enters 'yes', what will happen next?
found=False

while found==False:
  foundit=input("Have you found it?")
  if foundit=="yes":
    found=True
  else:
    found=False

print("Well done, you've found it!")

  the boolean flag is set to 'Yes', and as the condition is not met, it will go to line 8

  the boolean flag is set to True, exit the while loop to line 10

  the boolean flag is kept at False, the while loop continues

  the boolean flag is set to True, the while loop continues as the condition is not met

 9. Analyse the program below. What needs to happen in order for line 14 to be executed?
def main():
    tries=0 #this is a variable called tries set to start at 0 (initialised at zero)
    accessgranted=False 
    while accessgranted == False and tries<3: 
        password=input("Enter password:")
        if password=="open123":
            print("Access Granted")
            accessgranted=True
            
        else:
            print("Denied")
            tries=tries+1 
    if tries>=3: 
        print("Sorry, you have exceeded the number of tries. Goodbye")
        
main()

  accessgranted = True (the user has got the password right) and tries is >= 3

  accessgranted = False (the user got the password wrong) and tries is more than 1

  accessgranted = True (the user hasn't entered 'open123') and tries has exceeded 3

  accessgranted = False (the user hasn't entered 'open123') and tries is >=3

 10. In this loop, the while loop starts and goes on indefinitely until 'y' is pressed. What command is used to exit the loop?
while 1:
    print ('Spam')
    answer = input('Press y to end this loop')
    if answer == 'y':
        print ('Fries with that?')
        break
print ('Have a ')
print ('nice day!')

  Line 6: break

  Line 1: while 1 needs to be set to 0

  Line 4: answer

  Line 2: print('Spam')