Now that we have our counter working with the LEDs connected to the board, we want to add new features. We want to be able to easily transform a number between 1 and 9 into its representation in LEDs connected to the board.
The following lines show the code for the new NumberInLeds
class. The code file for the sample is iot_python_chapter_03_05.py
.
class NumberInLeds: def __init__(self): self.leds = [] for i in range(1, 10): led = Led(i) self.leds.append(led) def print_number(self, number): print("==== Turning on {0} LEDs ====".format(number)) for j in range(0, number): self.leds[j].turn_on() for k in range(number, 9): self.leds[k].turn_off()
The constructor, that is, the __init__
method, declares an empty list attribute named leds
(self.leds
). Then, a for
loop creates nine instances of the Led
class and each of them represent an LED connected...