In the previous lesson, we learned how to use loops and a bit of trigonometry to build a ring. In this lesson, we'll learn how to add color to it. The following image is what we're aiming for:
Let's see the code for the uncolored ring from the last lesson:
Throwing some color into each place
function call should be as simple as
picking out a color from ALL_COLORS
and adding it to place
. So maybe
the following should do:
What we did in the code above was to realize that position
gives us a
conveniently incrementing number that we can use to index into the
ALL_COLORS
list. So we just use it to select a color and apply it.
Let's see what happens when we increase the RADIUS
and the number of
SEGMENTS
.
If you ran the code above, you would have noticed that the ring is incomplete
and that there is an error. The error says IndexError: list index out of range on line 11
. At some point during the looping over the colors, the variable
position
equaled the length of ALL_COLORS
(let's call that N
for
now).
Remember that when position
becomes 0
, it can be used to index the first
element of the list. When it becomes N-1
, we access the last item of the
ALL_COLORS
list. So when position
becomes N
, it can't be used to
access anything in the list. Such a situation is called an IndexError
in
Python.
We could use some more looping to work around the issue:
As you may have noticed, the outer loop now has an inner loop of its own that
subtracts len(ALL_COLORS)
from a variable that starts off as position
until it becomes smaller than N
. What that essentially leads to is that we
progress along the list of colors and when we get to the end, we switch back to
the beginning of the list and start over.
The code we wrote above is more complicated than it needs to be. Remember that in software development, the simpler your code, the more maintainable and error free it is likely to be. There is a simpler way to do what we did above and that's using the modulo operator.
The modulo operator is used to get the remainder of the division of two numbers.
In Python, it's expressed using the %
symbol in the format a % b
, read as
a
modulo b
. As mentioned above, the result of the expression a % b
is
the remainder of a
divided by b
. Let's see some examples:
The modulo operator is great for iterating over a list if you want to loop back to the beginning after reaching the last item. Remember that list image from before?
Let's imagine us iterating over it using a variable i
. As i
progresses
from 0
to N - 1
, things work as before and we can index the list using
ALL_COLORS[i]
. But when i
becomes N
, it is outside of this list's
range. But i % N
never goes out of range. In fact, N % N
is 0
and
(N + 1) % N
is 1
.
To clarify this a bit more, I'm going to loop over a list multiple times using the modulo operator:
You'll find a copy of our last attempt to color the ring below. Try to simplify it using the modulo operator. hint: the inner loop needs to go.
© 2024 Abhin Chhabra | Sitemap