Python Forum

Full Version: functional LEDs in an array or list? // RPi user
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi! Total programming noob (not just in Python) here trying to make his code a bit shorter and more readable.

I'm trying to put functional LEDs in an array or list to make it easier to turn two or more on at the same time. However it ends in:
Error:
AttributeError: 'list' object has no attribute 'on'
The code for it is:
import time
from gpiozero import Buzzer
from gpiozero import LED

red = LED(2)
yellow = LED(3)
green = LED(4)
blue = LED(14)
buzzer = Buzzer(15)
leds = [red, yellow, green, blue]

password = int(input('xxxxxxxx: '))
if password != xxxx:
    print('xxxxxxxx')
    for i in range(3):
        red.on()
        buzzer.on()
        time.sleep(0.2)
        red.off()
        buzzer.off()
        time.sleep(0.2)
elif password == xxxx:
    print('xxxxxx')
    for i in range(3):
        leds.on()
        buzzer.on()
        time.sleep(0.2)
        leds.off()
        buzzer.off()
        time.sleep(0.2)
    green.on()
The error is in line 25 and 28. I tried searching for a solution or how to implement it correctly, however any array or list functon i find uses it for text data.

Am i overthinking it or missing something obvious?
leds is a list, to access an item in the list use square brackets and give an index leds[0]
to use it in your code give the index as i provided by the for loop, line 25 = leds[i].on() and line 28 = leds[i].off()
(Aug-21-2022, 03:18 PM)Yoriz Wrote: [ -> ]leds is a list, to access an item in the list use square brackets and give an index leds[0]
to use it in your code give the index as i provided by the for loop, line 25 = leds[i].on() and line 28 = leds[i].off()
Thank you! I don't know how i could miss something like that. I both love and dislike the "it was so obvious how couldn't i see it" feeling.
Back to work!
Instead of this:
    for i in range(3):
        leds.on()
        buzzer.on()
        time.sleep(0.2)
        leds.off()
        buzzer.off()
Do this:
    for led in leds[:3]:  # loop over the first three leds.
        led.on()
        buzzer.on()
        time.sleep(0.2)
        led.off()
        buzzer.off()
Or this:
    for i in range(3):
        leds[i].on()
        buzzer.on()
        time.sleep(0.2)
        leds[i].off()
        buzzer.off()
Of the two I prefer the one that uses a slice instead of range(). If you want to iterate over a list of LED's why iterate over a range of numbers, then use the numbers to get the LEDs?
(Aug-22-2022, 03:02 AM)deanhystad Wrote: [ -> ]Instead of this:
    for i in range(3):
        leds.on()
        buzzer.on()
        time.sleep(0.2)
        leds.off()
        buzzer.off()
Do this:
    for led in leds[:3]:  # loop over the first three leds.
        led.on()
        buzzer.on()
        time.sleep(0.2)
        led.off()
        buzzer.off()
Or this:
    for i in range(3):
        leds[i].on()
        buzzer.on()
        time.sleep(0.2)
        leds[i].off()
        buzzer.off()
Of the two I prefer the one that uses a slice instead of range(). If you want to iterate over a list of LED's why iterate over a range of numbers, then use the numbers to get the LEDs?

Well the general idea was that with every further step the program would turn on 1 LED less, leaving the ones from the previous steps on. I coded it in the most crude, barbaric way by copy-paste the (each next "i in range" has 1 less LED) and adjusted the if towhile != to help with another bug i found (skipping the input after two wrong anwsers and going to the next question.
Trust me, i am not joking when i say i'm a beginner. I'm a noob really and just the fact that it works makes gape in awe no less than my 3 year old sitting next to me Wink
Well done for getting it to work, good luck with your future projects.