Preview

4 - Iteration, For and While Loops, Mandelbrot

 1. The three programming constructs are: (3 marks)

 2. These two loops produce identical outputs. Try it yourself!
for i in range(5):
  print(i,"Hello there")
  
print() #this creates a space between outputs


i=0
while i <5:
  print(i,"Hello there")
  i=i+1

  FALSE

  TRUE

 3. Name two types of loops (2 mark)

 4. The programmer would like the output of the code to be as shown below. The numbers should start at 1 and go up to 5. What needs to be added to the code to make this happen?
(1, 'Hello there')
(2, 'Hello there')
(3, 'Hello there')
(4, 'Hello there')
(5, 'Hello there')

  Line 2 should be: print(i+1,"Hello there")

  Line 1 should be: for i in range(1+,5)

  Line 1 should be: for i in range(1,5):

  Line 2 should be: print(i+I,"Hello there")

 5. Iteration is the term given to the ________ of a block of statements (code) within a computer program. Basically, think loops!

  modularisation/modularising

  repetition/repeating

  deletion/deleting

  selection/selecting

 6. Read the excerpt below and fill in the blanks. Remember the terms 'condition controlled' and 'count controlled'.
A count-controlled loop iterates (loops/repeats)
the number of times specified in the loop.

With a condition-controlled loop the 
iteration occurs indefinitely as long as the 
specified condition is being met.

The following loop is a _____________________
controlled loop:

for i in range(10):
    print(i)

  both count and condition

  count

  condition

  neither count nor condition

 7. What in the following code needs to be changed for the program to ask the user to enter their password 5 times?
for i in range(3):
  password=input("Enter password:")

  Line 1 should be changed to: for i in range(<5):

  Line 1 should be: for i in range(5):

  Line 1 should be: while<5: for i in range(5):

  Line 1 should be indented further

 8. The programmer wants to draw another black circle after the last purple circle. What code does he need to add?
import turtle
tina = turtle.Turtle()
tina.shape('turtle')

colors = ["red", "orange", "yellow", "green", "blue", "purple"]

for each_color in colors:
    angle = 360 / len(colors)
    tina.color(each_color)
    tina.circle(40)
    tina.right(angle)
    tina.forward(30)

  Line 5: Add "black" to the end of the list of colors (colours if you're in the UK!)

  Line 5: replace the word "colors" with "black"

  Add a line 13: tina.forward("black circle")

  Line 9: Change to - tina.color(each_color+black)

 9. In the code above point out which line, if at all, is using iteration.

  There is no iteration in this program

  Line 5

  Line 7

  Line 1

 10. The following code produces an output of: 2,3,4,5,6,7,8,9. What needs to change so that it produces an output of 1 to 10?
for i in range(2,10):
  print(i)

  Change line 2 to: print(1,10,i)

  Change line 1 to: for i in range(1,10):

  Change line 1 to: for i in range(1,11):

  Change line 1 to: for i in range(11):

 11. The following code is repeating the creation of squares, each inside the other, but it stops at count = 50. What do you need to change to make it go all the way up to 100 and complete the pattern?
import turtle

myPen = turtle.Turtle()
myPen.speed(0)
myPen.color("#FF0000")

side=400
myPen.penup()
myPen.goto(-200,-200) #position cursor at the bootom right of the screen
myPen.pendown()

#Start Spiral
for i in range (1,50):
   myPen.forward(side)
   myPen.left(90)
   side=side-4

  Change line 13 to: for i in range (1,100):

  Add a line 17: myPen.forward(100)

  Change line 7 to: side=400+100

  Change line 13 to: for i in range (50+):

 12. A while loop is different from a for loop in that it is ____________ controlled rather than _______ controlled.

  count / condition

  count / increment

  condition / count

  for / while

 13. The first for loop has a specified starting and stopping condition. Which statement is true of the while loop? (Try it yourself if you're unsure)
#For loop - start value is 1, and stopping condition is 11
for score in range(1,11):
	print(score)
  #the above loop prints 1,2,3,4,5,6,7,8,9,10
 
 
#While loop
score=0
while score <11:
  print(score)
    

  All the statements listed here are correct

  It has a starting value of 0. It also has a stopping condition of 11.

  The score starts at 0 and remains 0 for infinity (forever) because there is no incrementation

  The loop goes on forever. The score remains 0.

 14. In the code below, what needs to be added/changed to the while loop in order to make it print the numbers 1,2,3,4,5,6,7,8,9,10.
#While loop
score=0
while score <10:#what needs changing here?
 #what should go here?
  print(score)

  This cannot be achieved

  Line 3 should be: while score >10: and line 4 should be: score=1

  Line 3 should be: while score = 11: and line 4 should be: score = score+1

  Line 3 should be: while score <10: and line 4 should be: score=score+1

 15. In certain situations a while loop may never run. What needs to change to enable this while loop to run? Select the statement that is entirely correct.
password="open123"
while password=="123":
  print("Access granted")
    

  Change line 2 to: while password=="open123": Note however, this will cause an infinite loop.

  None of these answers are correct

  Change line 2 to: while password=password: The loop will then print "Access granted" once.

  Change line 2 to: while password==True: The loop will then print the numbers 1,2,3

 16. Why will the following while loop fail to run? Becaue the condition on line 2 is __________.
x=22
while x<10:
  print("This")

  is met and it would cause an error.

  never met due to the syntax error in the loop. The colon should be removed.

  is never met becaue the starting value of x is 22 and the loop requires it to be less than 10.

  never met due to the use of the 'while' statement. A 'for' statement should be used instead.

 17. The following code will not run! What one thing do you need to change in order for it to work properly?
health=100
gameover=True
while gameover==False:
  health_update=int(input("Enter current health:"))
  health=health_update
  if health>20:
    print("You are healthy")
  else:
    print("Your health is critical.....ahhhhhhhhh")
    gameover=True
    

  Change line 2: gameover=False

  Change line 1: health=0

  Change line 3: while gameover==0:

  Change line 10: gameover=False

 18. Have a look at lines 8 and 9. How would you describe what has been coded here?
import turtle

myPen = turtle.Turtle()
myPen.speed(0)
myPen.color("#FF0000")


for j in range (1,100):
  for i in range (1,6):
      myPen.left(144)
      myPen.forward(200)
  myPen.left(5)

  A nested loop (for loop) has been used

  This is called decomposition as the for loop has been duplicatedand effectively cancelled out

  There is an error - you cannot have one for loop inside another

  There are two for loops that are running at exactly the same time. An error is output

 19. On line 2 what is the != operator?
password = ""
while password != "secret":
    password = input("Please enter the password: ")
    if password == "secret":
        print("Thank you. You have entered the correct password")
    else:
        print("Sorry the value entered in incorrect - try again")

  equal to as long as it does not contain an exclamation mark

  equals to

  equals to any word that also contains an exclamation mark

  not equals to

 20. Predit the output
for i in range(2):
  while i<3:
    print(i)

  It will output an error. You cannot have a while loop inside a for loop

  It will print 1,2,3 in an infinite loop

  It will print 0,1,2,3

  It will print 0 forever (infinite loop)