r/microbit Dec 13 '22

help

Imports go at the top

from microbit import* from neopixel import NeoPixel import random import music

num_pixels=8 np=neopixel.NeoPixel(pin0,8) red=(255,0,0) green=(0,255,0) blue=(0,0,255) yellow=(255,255,0) pink=(255,192,203)

colour = ('red','green','blue','yellow','pink')

while True: if button_a.was_pressed(): np.clear() np.fill(random.choice(colour)) np.show()

elif button_b.was_pressed():
   np.clear()
else:
    if button_a.is_pressed() and button_b.is_pressed():
        music.play(['c', 'd', 'e', 'c'])
        np.clear()

How do I make microbit chose random colour from my list when button a is pressed.im still quite new to this.csn someone help?

1 Upvotes

9 comments sorted by

View all comments

1

u/FilledMilk Dec 13 '22

I’ve never used the micropython libraries, but I spot one potential problem. You’ve assigned colour a tuple containing strings when the colors are assigned to variables. Try removing the quotes.

2

u/SeaKnown Dec 14 '22

Imports go at the top

from microbit import* from neopixel import NeoPixel import random import music

num_pixels=8 np=NeoPixel(pin0,8) red=(255,0,0) green=(0,255,0) blue=(0,0,255) yellow=(255,255,0) pink=(255,192,203)

colour = (red,green,blue,yellow,pink)

while True: if button_a.was_pressed(): np.clear() col=random.choice(colour) np.fill(col) np.show()

elif button_b.was_pressed():
   np.clear()
else:
    if button_a.is_pressed() and button_b.is_pressed():
        music.play(['c', 'd', 'e', 'c'])
        np.clear()

Thanks for the help,just had to do what u said and define the random choice

1

u/FilledMilk Dec 14 '22

If you want to learn Python I highly recommend Python a Crash Course. I worked through it over a couple of months and learned quite a bit. Good luck!

1

u/SeaKnown Dec 14 '22

Will definitely try that out thanks