I'm starting with python and I do not understand one thing. Help

1

I am following a course for beginners. In the following example I do not understand the use of x% in the line

  

t.pencolor (colors [x% len (family)]) # Rotate through the colors

If someone can explain it to me, I would appreciate it.

I leave the entire program here.

# SpiralFamily.py - prints a colorful spiral of names
import turtle     # Set up turtle graphics
t = turtle.Pen()  
turtle.bgcolor("black")
colors = ["red", "yellow", "blue", "green", "orange",
        "purple", "white", "brown", "gray", "pink" ]
family = []       # Set up an empty list for family names
# Ask for the first name
name = turtle.textinput("My family",
                        "Enter a name, or just hit [ENTER] to end:")
# Keep asking for names
while name != "":
    # Add their name to the family list
    family.append(name)
    # Ask for another name, or end
    name = turtle.textinput("My family",
                        "Enter a name, or just hit [ENTER] to end:")
# Draw a spiral of the names on the screen
for x in range(100):
    t.pencolor(colors[x%len(family)]) # Rotate through the colors
    t.penup()                         # Don't draw the regular spiral lines
    t.forward(x*4)                    # Just move the turtle on the screen
    t.pendown()                       # Draw the next family member's name
    t.write(family[x%len(family)], font = ("Arial", int((x+4)/4), "bold") )
    t.left(360/len(family) + 2)         # Turn left for our spiral
    
asked by Francesc 05.04.2018 в 16:47
source

2 answers

0

EDIT

operator modulo / remainder (remainder) % which exists in almost all languages, basically is a division between integers and returns the remainder. As long as the numerator is greater than or equal to the denominator , the remainder will always be between 0 and 9 so when doing

x%len(family) // retorna entre 0 y 9

Use this to ensure that you always give a "random" number between 0 and 9 to access the colors arrangement that is size 10, so you will always be choosing a random index that will not go beyond the limits of the arrangement.

PD: len(array) returns the size of an array

    
answered by 05.04.2018 в 17:21
0

The operator % returns the rest of a division, so for example x%2 will return 0 if x is even, or 1 if odd.

In general, the rest of the division A%B will be a number between 0 and B-1 .

It is very typical to use this operation to make sure that your number never goes beyond a certain range. For example, if you want to have an accountant that goes up one by one, but if you reach 50 again start with zero, a reasonable way to do it would be:

contador = contador + 1
if contador == 50:
   contador = 0

But another shorter way, using the operator % would be:

contador = (contador + 1) % 50

Notice that as soon as the counter exceeds 50, by staying with the rest of your division between 50 "starts again with zero." This works even if you increase it in any other way instead of increasing it by 1 in 1. Whenever you pass 50 "enter again below" (53 would become 3, etc.)

This use is also given to access different elements of an array in a cyclical way, that is, once we have chosen the last element, we start again with the first one.

That's what the code of your example is about ( which by the way has a bug , because instead of len(family) you should use len(colors) ):

colors = ["red", "yellow", "blue", "green", "orange",
        "purple", "white", "brown", "gray", "pink" ]
# ...
for x in range(100):
    t.pencolor(colors[x%len(colors)])

Notice that the x varies between 0 and 100, and in each iteration we want to use a different color from the list colors , in which there are only 10 elements. If we simply put colors[x] , when the x reaches the value 10 we would be leaving the list (since its indexes range from 0 to 9). With the trick of the operator % , we ensure that we are always accessing valid elements (between 0 and len(colors)-1 ).

In this way the colors are accessed in sequence, in a cyclical way (once the "pink" is reached, the next one would be again "red" ).

    
answered by 06.04.2018 в 14:10