3. Loops Practice Quiz

  • CS20-FP2 Investigate how control structures affect program flow.

To confirm that you understand the major concepts you’ve seen in Python, try to answer the following questions without opening Python.

3.1. Question 1

    loops-quiz1: What shape will the turtle alex draw when the code below is executed?:

    import turtle
    
    theWindow = turtle.Screen()
    theWindow.bgcolor("lightgreen")
    alex = turtle.Turtle()
    alex.pensize(3)
    
    for i in [0,1,2,3]:
        alex.forward(50)
    alex.left(90)
    
  • No shape will be drawn.
  • Try again!
  • A line segment.
  • Great!
  • A triangle.
  • Try again!
  • A square.
  • Try again! Notice that that alex.left(90) command is not inside the for loop.

3.2. Question 2

    loops-quiz2: What shape will the turtle alex draw when the code below is executed?:

    import turtle
    
    theWindow = turtle.Screen()
    theWindow.bgcolor("lightgreen")
    alex = turtle.Turtle()
    alex.pensize(3)
    
    for i in [0,1,2,3]:
        alex.forward(50)
        alex.left(90)
    
  • No shape will be drawn.
  • Try again!
  • A line segment.
  • Try again! This time, the alex.left(90) is included in the for loop.
  • A triangle.
  • Try again!
  • A square.
  • Great!

3.3. Question 3

loops-quiz3: In the following code, how many lines does this code print?:

for number in [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]:
    print("I have", number, "cookies.  I'm going to eat one.")

3.4. Question 4

    loops-quiz4: The following will print a line showing the number 4:

    for i in range(4):
        print(i)
    
  • True
  • Nope. Remember that range(4) will create a list with elements [0,1,2,3].
  • False
  • Great!

3.5. Question 5

loops-quiz5: What is the last line that this code will print?:

i = 1
while (i <= 3):
    i = i + 1
    print(i)
Next Section - 4. Functions Practice Quiz