How can I find the remainder when dividing 2 integers

S

silverburgh.meryl

I have a string array:
colors = ["#ff0000", "#00FF00", "#0000FF"]
colorIndex = 0;

and I want to loop thru each element of colors

for str in strings:
print colors[colorIndex++ % colors.length]


But i get an invalid syntax error when I execute the script:
print colors[colorIndex++ % colors.length]
^
SyntaxError: invalid syntax
 
D

Diez B. Roggisch

I have a string array:
colors = ["#ff0000", "#00FF00", "#0000FF"]
colorIndex = 0;

and I want to loop thru each element of colors

for str in strings:
print colors[colorIndex++ % colors.length]


But i get an invalid syntax error when I execute the script:
print colors[colorIndex++ % colors.length]
^
SyntaxError: invalid syntax


Python doesn't have the post-decrement-operator. Also not a pre-increment.

You have to put a statement like

colorIndex += 1

in your loop.

Diez
 
D

David

I have a string array:
colors = ["#ff0000", "#00FF00", "#0000FF"]
colorIndex = 0;

and I want to loop thru each element of colors

for str in strings:
print colors[colorIndex++ % colors.length]


But i get an invalid syntax error when I execute the script:
print colors[colorIndex++ % colors.length]
^
SyntaxError: invalid syntax
The syntax error comes from your use of the ++ operator not the modulo
operator. The ++ operator is not valid in python.

david lees
 
S

silverburgh.meryl

okay, I try you suggestion, and re-write my code like this:
colors = ["#ff0000", "#00FF00", "#0000FF"]
colorIndex = 0

def getText(nodelist):


for str in strings:

print colors[colorIndex % colors.length]
colorIndex += 1

but i get this error:
print colors[colorIndex % colors.length]
UnboundLocalError: local variable 'colorIndex' referenced before
assignment
 
D

Diez B. Roggisch

okay, I try you suggestion, and re-write my code like this:
colors = ["#ff0000", "#00FF00", "#0000FF"]
colorIndex = 0

def getText(nodelist):


for str in strings:

print colors[colorIndex % colors.length]
colorIndex += 1

but i get this error:
print colors[colorIndex % colors.length]
UnboundLocalError: local variable 'colorIndex' referenced before
assignment

If I take your code, it works for me.

Diez
 
S

silverburgh.meryl

Can you please tell me what is the meaning of
UnboundLocalError: local variable 'colorIndex' referenced before
assignment

in general?
 
D

Diez B. Roggisch

Can you please tell me what is the meaning of
UnboundLocalError: local variable 'colorIndex' referenced before
assignment

in general?

Well, pretty much of what it says: You tried to access a variable without prior assignment to it. Like this:


a = b**2 + c**2

Won't work. But if you do

b = 2
c = 3
a = b**2 + c**2

it works. I suggest you read a python tutorial - plenty of the out there, google is as always your friend.

Diez
 
K

Kent Johnson

Diez said:
okay, I try you suggestion, and re-write my code like this:
colors = ["#ff0000", "#00FF00", "#0000FF"]
colorIndex = 0

def getText(nodelist):


for str in strings:

print colors[colorIndex % colors.length]
colorIndex += 1

but i get this error:
print colors[colorIndex % colors.length]
UnboundLocalError: local variable 'colorIndex' referenced before
assignment


If I take your code, it works for me.

Probably because it never calls getText().

The problem is that colorIndex is being assigned inside getText(). This
makes Python assume it is a local variable and it won't see the global
colorIndex. The UnboundLocalError is telling you that there is no value
(binding) for the local variable colorIndex.

One solution is to move colorIndex into getText() and initialize it
every time:

def getText(nodelist):
colorIndex = 0
for str in strings:
print colors[colorIndex % colors.length]
colorIndex += 1

If you want the value of colorIndex to persist so one call to getText()
picks up where the last one left off, put a global statement inside
getText() so Python knows colorIndex is global:

def getText(nodelist):
global colorIndex
for str in strings:
print colors[colorIndex % colors.length]
colorIndex += 1

Kent
 
D

Dennis Lee Bieber

I have a string array:
colors = ["#ff0000", "#00FF00", "#0000FF"]
colorIndex = 0;

and I want to loop thru each element of colors

for str in strings:
print colors[colorIndex++ % colors.length]

What purpose has "str" or "strings", you use neither within the
loop. If the sole purpose is to control the number of iterations then...

for i in range(len(strings)):
print colors[i % len(colors)]

.... does everything, with no need for a separate index counter (NOTE:
there is no "length" attribute for arrays, you need to invoke the len()
function on them)
--
 
S

Steven D'Aprano

Well, pretty much of what it says: You tried to access a variable without prior assignment to it. Like this:


a = b**2 + c**2

Won't work. But if you do

b = 2
c = 3
a = b**2 + c**2

it works. I suggest you read a python tutorial - plenty of the out there, google is as always your friend.


Diez' advice is good, but his example is wrong: it will raise a NameError
exception.

When you have a function like this:

def foo(x):
z = x + y
return z

Python's scoping rules treat x and z as local variables, and y as a
global variable. So long as y exists, the function will work.

When you do this:

def bar(x):
y = 2
z = x + y
return z

the scoping rules treat y as a local variable, because you assigned to it.

But with this:

def baz(x)
z = x + y
y = 2
return z

the scoping rules see the assignment to y at compile time, so y is treated
as a local variable. But at run time, y is accessed before it has a value
assigned to it: UnboundLocalError

Hope this helps.
 
A

Andrea Griffini

Writing a while loop with ++x to increment the index was the first
mistake i made with python.
"++x" unfortunately is valid, it's not a single operator but a double
"unary plus"

Andrea
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top