14.4. Using Random Numbers¶
We can generate random numbers in Python using the
randrange function in the
random library. This function takes an optional starting value (inclusive) and the ending value for the range (exclusive). We can use random numbers in games to add an element of chance. We can also use random numbers to move the turtle to random positions as shown below. We are using conditionals to alternate the drawing color each time the turtle moves.
Can you modify the code above to use 3 different colors? You can use
num % 3 to give you 3 possible results.
In the first section this chapter we created the chevron image on the left using turtles. Modify the code to create the stampped image on the right. The turtles will stamp/draw in blue and green. The color will be chosen at random.
Discuss topics in this section with classmates.