We can program the computer to behave randomly. Python has a module called random that allows us to simulate random choices.
Guessing
Colorful Random Spirals
Rock-Paper-Scissors
Pick a Card
Roll the Dice
Keleidoscope
Summary
At this point, you should be able to do the following:
- Import the random module into your programs.
- Use random.randint() to generate a random integer number in a given range.
- Use random.choice() to pick a value at random out of a list or array.
- Use random.choice() to generate 52 card values from two arrays of strings containing only the faces and suits.
- Determine the size of your drawing window with turtle.window_ width() and turtle.window_height().
- Move the turtle to any position on the drawing screen with turtle.setpos(x,y).
- Use the random.randrange() function to generate a random number in any range.
- Find the index of an element in a list or array with the .index() function.
- Build a while game loop using a Boolean flag variable like keep_going.
- Construct an array of similar types of values, assign values to elements in the array by their index (as in dice[0] = 2), and use array elements like regular variables.
- Sort lists or arrays with the .sort() function.
- Reflect points about the x- and y-axes by changing the signs of the points’ (x, y) coordinate values.
- Change the turtle’s drawing speed with the .speed() function.