473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How can I find the remainder when dividing 2 integers

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

Feb 26 '06 #1
10 2427
si***************@gmail.com schrieb:
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
Feb 26 '06 #2
si***************@gmail.com wrote:
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
Feb 26 '06 #3
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

Feb 26 '06 #4
si***************@gmail.com schrieb:
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
Feb 26 '06 #5
Can you please tell me what is the meaning of
UnboundLocalError: local variable 'colorIndex' referenced before
assignment

in general?

Feb 26 '06 #6
si***************@gmail.com schrieb:
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

Feb 26 '06 #7
Diez B. Roggisch wrote:
si***************@gmail.com schrieb:
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
Feb 26 '06 #8
On Sun, 26 Feb 2006 21:58:30 +0100, Diez B. Roggisch wrote:
si***************@gmail.com schrieb:
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' 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.

--
Steven.

Feb 26 '06 #9
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

Feb 26 '06 #10
Learn about ther itertools module, one of the most useful and elegant
modules in the standard library
(http://docs.python.org/lib/module-itertools.html):

import itertools as it
colors = ["#ff0000", "#00FF00", "#0000FF"]
words = "Itertools is a pretty neat module".split()
for word_color in it.izip(word, it.cycle(colors)):
print "<font color=%s>%s</font>" % word_color

George

Mar 2 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Justin Hoffman | last post by:
This is a question concerning query optimisation. Sorry if it's a bit long, but thanks to anyone who has the patience to help - This is my first post here... If I have two tables:...
3
by: gh | last post by:
I am dividing var1 by var2 and I want to check and see if there is a remainder. How do I do this in C#? TIA
21
by: Imran | last post by:
I have a vector of integers, such as and I want to find out the number which occurs most frequently.what is the quick method. My array size is huge. what I am doing is 1. find out the...
16
by: Petrakid | last post by:
Hey, I have a task to complete. I am trying to figure out the best way, in C++ to determine the following. There is this farm with pigs and chickens. Only the legs of the pigs and chickens look...
9
by: nick048 | last post by:
Hi to all, If in input I enter an int, I need to create a function that verify if my int is in a list of this value: 1 4 7 10 13 16 19 22 25 28 31 34. How can resolve this question. I hope in...
11
by: redefined.horizons | last post by:
I'm still pretty new to Python. I'm writing a function that accepts thre integers as arguments. I need to divide the first integer by te second integer, and get a float as a result. I don't want...
2
by: pshemu | last post by:
hi, is there a way to fast calculate x-x%y (greatest multiplication of y before x), where x and y are 64-bit integers on a 32-bit machine?? Maybe MMX registers would be useful?? I am writing...
34
by: linq936 | last post by:
Hi, I just did the following test: 1:void main(void){ 2: int p = 1; 3: int* pp = &p; 4: int c = p * pp; 5: int d = p + pp; 6:}
4
by: Giff | last post by:
Hi, I have a function that takes in a float and then performs a division. Since I know that it is impossible to check if the value I pass is equal to zero (being a float), I don't perform any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.