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.
Question 1¶
- No shape will be drawn.
- Try again! The turtle will move when iterating through the for loop.
- A line segment.
- Great!
- A triangle.
- Try again! Think about how many times the loop will iterate.
- A square.
- Try again! Notice that that alex.left(90) command is not inside the for loop.
loops-quiz1: What shape will the turtle alex draw when the code below is executed?:
import turtle
the_window = turtle.Screen()
the_window.bgcolor("lightgreen")
alex = turtle.Turtle()
alex.pensize(3)
for i in [0,1,2,3]:
alex.forward(50)
alex.left(90)
Question 2¶
- No shape will be drawn.
- Try again! Think about how many times the loop will iterate.
- A line segment.
- Try again! This time, the alex.left(90) is included in the for loop.
- A triangle.
- Try again! Think about how many times the loop will iterate.
- A square.
- Great!
loops-quiz2: What shape will the turtle alex draw when the code below is executed?:
import turtle
the_window = turtle.Screen()
the_window.bgcolor("lightgreen")
alex = turtle.Turtle()
alex.pensize(3)
for i in [0,1,2,3]:
alex.forward(50)
alex.left(90)
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.")
Question 4¶
- True
- Nope. Remember that range(4) will create a list with elements [0,1,2,3].
- False
- Great!
loops-quiz4: The following will print a line showing the number 4:
for i in range(4):
print(i)
Question 5¶
loops-quiz5: What is the last line that this code will print?:
i = 1
while (i <= 3):
i = i + 1
print(i)