[Tutor] Set and get class members

Phil phillor9 at gmail.com
Wed Jul 21 02:27:53 EDT 2021


On 19/7/21 8:35 pm, Alan Gauld via Tutor wrote:
>
> My personal recommendation is to keep it as simple as possible.
> Access the state attribute directly and have the LEDs stored
> in a global list variable.

This is what has been suggested;

         self.leds = list()
         for i in range(8):
             self.leds[i].self.state = False # this is not correct. If state is True then the LED is on.
             self.leds.append(Led((50 + (i * 30), 50))) # this results in 8 equally spaced LEDs

Is this what you have in mind?

No matter how I try to set the state of a LED the error message is:

IndexError: list assignment index out of range

This is how I came up with a working solution in C++ using set methods 
quite some time ago.

   l = new Led[num_leds];


   for (int i = 0; i < num_leds; i++)
   {
     l[i] = new Led(60 + (i * 40), 120);
     l[i].setState(false);
     l[i].setLedSize(30);
     l[i].setOnColour(green);
     l[i].setOffColour(black);
   }

All I need to do is set the state of say LED[3].

I've been sitting in a broken down motorhome for most of the day and my 
head is in a bit of a spin which could be why I'm having trouble with 
this simple problem.

-- 
Regards,
Phil



More information about the Tutor mailing list