Here is the template you built so far, with a few tiny changes.

#!/usr/bin/env pybricks-micropython 
from pybricks import ev3brick as brick
from pybricks.parameters import Button, Color
from pybricks.tools
import wait
import time
import random
randomList = [] colorList = [Color.RED, Color.YELLOW, Color.ORANGE, Color.GREEN, Color.BLACK] def blink(): brick.light(Color.BLACK)
wait(500)
answerList = []
def randomSet():
global randomList
# RED = LEFT button
# YELLOW = RIGHT button
# ORANGE = DOWN button
# GREEN = UP button
# BLACK = starting mode
for loop in range(4):
randomList.append(randomColor())
wait(1000)
def randomColor():
seedling = int(round(time.time()))
random.seed(seedling)
colorNo = int(random.randint(0,100) / 25)
return(colorNo)
randomSet()
print(“randomList”,randomList)
blink()
for colorNo in randomList:
brick.light(colorList[colorNo])
wait(1000)
blink()

The colorNo random line changed and we added an answerList variable.

Now I didn’t include the while True: loop cause that has changed quite a bit. What we want to do is associate a sequence with the buttons. The comment explains the plan.

LEFT will return RED,
RIGHT will return YELLOW,
DOWN will return ORANGE and
UP will return GREEN.

We will create an answerList as we press the keys. When we have enough elements we’ll exit and compare the randomList with the answerList.

If they are the same, we will flash green if they are different we will flash red.

Firstly we need to add two more subroutines, one to blink green and one to blink red.

def blinkGreen(): 
for loop in range(4):
brick.light(Color.GREEN)
wait(500)
blink()
def blinkRed():
for loop in range(4):
brick.light(Color.RED)
wait(500)
blink()

Then we need to add an entirely new while loop.

while len(answerList) != len(randomList): 
if Button.LEFT in brick.buttons():
color = colorList[0]
brick.light(colorList[0])
answerList.append(0)
wait(500)
if Button.RIGHT in brick.buttons():
color = colorList[1]
brick.light(colorList[1])
answerList.append(1)
wait(500)
if Button.UP in brick.buttons():
color = colorList[2]
brick.light(colorList[2])
answerList.append(2)
wait(500)
if Button.DOWN in brick.buttons():
color = colorList[3]
brick.light(colorList[3])
answerList.append(3)
wait(500)

This isn’t that different from the last one really. This time we’re showing specific colors when we hit specific keys and we’re adding the keys pressed to an answerList.

Finally we need to compare the our answer, with the one initially presented.

if answerList == randomList: 
blinkGreen()
else:
blinkRed()

Add this code under the while loop. Make sure it isn’t indented, it needs to run after the loop exits.

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store