I'm trying to program a Whack a mole game using four LEDS with four corresponding buttons, but I'm getting stuck with how to make one random LED turn on at a time. Here's the code I have so far:

from rstem.button import Button
from rstem.gpio import Output
import time

my_buttons = [Button(15), Button(23), Button(25), Button(16)]
my_led = [Output(14), Output(18), Output(24), Output(12)]
mode = 1
was_pressed = False

while True:
    for button in my_buttons:
        if button.is_pressed():
            mode = mode - 1
            if mode > NUM_MODES:
            mode = 1

        if mode == 0:
        elif mode == 1:

        was_pressed = is_pressed
  • Also, I know that it should be my_button and not my_buttons, but disregarding that mistake, since my_button is a list, it won't let me give it the attribute of on from my_led.on().
    – Alex I
    Mar 27, 2017 at 0:13
  • Please edit your question and indent all of your code by four spaces. As you can tell, without it all indented, it doesn't render properly.
    – stevieb
    Mar 27, 2017 at 0:27
  • Your code is 95% unfinished and with my_led.off() and my_led.on() it won't even run. I can see several ways how one could write a program that would do as you describe. But since so many things are wrong or missing in your code, I have a hard time identifying the next good step to learn for you. Well here goes my hint what to try next: You need to pair your buttons and leds somehow. The most straight-forward way would be to iterate over range(4) and then fetch button and led by index like for i in range(4): led = [my_leds[i]; button = my_buttons[i]; Try that and update your question.
    – MadMike
    Mar 27, 2017 at 7:01

2 Answers 2


To make a random LED turn on you should use the random library. With the function random.choice(my_led) you can select one of your LED's randomly from the list. That random LED can then be turned on.

However, it is necessary to connect each LED to a button. This makes it easier to program the pi to turn off a certain LED when the corresponding button is pressed. You can do this by putting the button that corresponds to an LED in the same position in the my_buttons list as the LED in the my_led list.

I would reccomend using the ramdom.randint(least, largest) function to generate a random interger between -1 and 4. This interger, 0,1,2 or 3, can be stored in a variable. This variable can be used to select an LED from the my_led list and the corresponding button from the my_buttons list. The selected LED can the be turned on. After that, the button belonging to that LED can be checked a few times with a for loop with a small delay in each itteration of the loop.

In conclusion, you should look into the random library to generate a random LED.

I hope this helps,

Julian Sanders

  • "randint(least, largest)" -> Implies the range is inclusive, whereas your choice of -1 and 4 implies it is exclusive. I'm not a user of the API, but if you copied that from documentation you may want to double check which way it is (a combination is common whereby it will be inclusive of the first number but exclusive of the last).
    – goldilocks
    Mar 27, 2017 at 16:57

I am doing a similar project and just today found the best way for me to do it. (FYI-I am an intermediate beginner so take this for what it is worth). Here is the code I used:

import random
from random import shuffle

For lightLocation in Lights:
     print (lightLocation)

note: I added each light three times and these numbers are the GPIO positions

note: prints the shuffled list so I could make sure it worked

That was my test program, then I rolled that into my main program as appropriate. Seems to work pretty well.

My next task is trying to do it with illuminated buttons.....working on finding that hardware which I asked on another forum. Idea is a button lights up, you press it, it goes off, next one lights up. Exactly what you are doing but I don't want a separate light and button.

  • Hello and welcome to Raspberry Pi! Please take the tour and visit the helpcenter to see how things work here. I think the shuffle line should read Lights = shuffle(Lights) and note the proper capitalization of for not For. Additionally, if I may say, I would use lower case variable names (lights instead of Lights, see PEP8 for more).
    – Ghanima
    May 3, 2017 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.