In this project, we continue the development of our Simon Says pattern-matching game by adding randomized LED blinking.

In our last project, we wrote some code to flash LEDs in a preplanned sequence. While that's a good start, in our Simon Says game, we're going to need to flash LEDs in a random sequence. This will require us to make some modifications to the code from the last project, which will give us a random sequence of LED flashes.

The hardware setup for this project should be the same setup that was completed for the project. Here's a reminder:

Here is what the code for this project should look like at this point (with new changes highlighted with arrows). When you run it, you will see a random LED flash, then that light will repeat with a second random light added, then that 2-light sequence will repeat with a third added and so on (the sequence will continue until you stop the program):

# ################################## # Import Modules and Initialize Game # ################################## from rstem.gpio import Output *from random import randrange import time lights = [Output(4), Output(18), Output(24), Output(27)] for light in lights: light.off() *play_order = [] # ################################## # Main while: loop for game # ################################## *while True: * # Add another random LED to sequence * play_order.append(randrange(4)) # Play LED sequence for i in play_order: lights[i].on() time.sleep(0.4) lights[i].off() time.sleep(0.2)

Here, we've made the following code changes:

- On Line 6, we import the `randrange()` we'll need later.

- On Line 14, instead of filling the `play_order` list with our predefined sequence of LED flashes, we're going to create an empty list that we can fill with a random sequence of flashes.

- On Line 20, we add a `while` loop, so that we can continue to generate a random LED flash and add it to the list each time through the loop. This new loop will play the entire sequence of random LED flashes, add a new flash to the sequence, play the list again with the new LED flash added, and then repeat.

- On Line 23, we append another random LED flash to the `play_order` list sequence.