Python Forum
functional LEDs in an array or list? // RPi user
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
functional LEDs in an array or list? // RPi user
#1
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?
Reply
#2
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()
Reply
#3
(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!
Reply
#4
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?
Reply
#5
(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
Reply
#6
Well done for getting it to work, good luck with your future projects.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Question How to let the user add 'None' to an specific array in a dictionary? noahverner1995 4 1,735 Dec-26-2021, 10:03 AM
Last Post: noahverner1995
  Stuck in functional proggaming haze hammer 2 1,374 Oct-27-2021, 02:07 PM
Last Post: hammer
  Make these LEDs stay on duckredbeard 5 2,914 Apr-08-2021, 10:26 PM
Last Post: duckredbeard
  Looking for a way to loop until user enters from a list? PythonW19 7 3,267 Mar-21-2021, 08:56 PM
Last Post: PythonW19
  OOP vs functional - - elaborate turn based RPG game (Derek Banas Udemy course again) Drone4four 6 3,868 Mar-14-2021, 08:38 AM
Last Post: ndc85430
  Calculating surface area - - OOP or functional? Derek Banas Udemy course Drone4four 5 3,529 Mar-13-2021, 06:22 AM
Last Post: buran
  User input/picking from a list AnunnakiKungFu 2 2,284 Feb-27-2021, 12:10 AM
Last Post: BashBedlam
  LIST or ARRAY Comparison and change of value nio74maz 0 1,669 Dec-21-2020, 05:52 PM
Last Post: nio74maz
Question Reset list if user regrets Gilush 1 2,044 Dec-05-2020, 10:55 AM
Last Post: Gilush

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020