One more thing: Our programs often featured repetition. There is a powerful concept in Python called looping, which we will elaborate later on. For now, try this easy example:
for i in range(10):
print("Hello!")
This is incredibly helpful if we want to do something multiple times — say, drawing the individual border lines of a shape — but only want to write it once. But it gets better:
for i in range(10):
print(i)
Notice how we write only one line of code using i but it has 10 different values?
You can also loop over elements of your choice:
for i in 5, 7, 11, 13:
print(i)
The range(n) function can be considered a shorthand for 0, 1, 2, ..., n-1. If you want to know more about it, you can use the help in the Python shell by typing help(range). Use the q key to exit the help again.
Draw a dashed line. You can move the turtle without tracing a line behind you with the turtle.penup() function; put it back on the ground with turtle.pendown().
for i in range(10):
turtle.forward(15)
turtle.penup()
turtle.forward(5)
turtle.pendown()
Can you make the dashes become larger as the line progresses?
Hint
Feeling lost? Inspect i at every run of the loop:
for i in range(10):
print(i)
turtle.forward(15)
turtle.penup()
turtle.forward(5)
turtle.pendown()
Can you utilize i — commonly called the index variable or loop variable — to get increasing step sizes?