473,770 Members | 3,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2450
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(nodelis t):
for str in strings:

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

but i get this error:
print colors[colorIndex % colors.length]
UnboundLocalErr or: 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(nodelis t):
for str in strings:

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

but i get this error:
print colors[colorIndex % colors.length]
UnboundLocalErr or: 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
UnboundLocalErr or: 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
UnboundLocalErr or: 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(nodelis t):
for str in strings:

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

but i get this error:
print colors[colorIndex % colors.length]
UnboundLocalErr or: 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 UnboundLocalErr or 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(nodelis t):
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(nodelis t):
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
UnboundLocalErr or: 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: UnboundLocalErr or

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

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

Similar topics

16
2083
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: 'tblContact' and 'tblCategory' where categories are like: Code Name 010101 Short 010102 Fat
3
16062
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
8152
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 maximum value N 2. loop through 1...N 3. count # times each occurred
16
4465
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 exactly the same, so for a short person to determine how many possible pigs and how many possible chickens, all they can do is count the total legs of the two. - Chickens seem to always run around in groups of 3 - Pigs seem to always clump...
9
1578
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 Your help. Best Regards Gaetano
11
15197
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 the caller of the function to have to pass floats instead of integers. How do I convert the arguments passed to the function into floats before I do the division? Is this necessary, or is their a better way? Thanks, Scott Huey
2
1842
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 this with gcc and gas. Any help would be appreciated and thanks in advance. Best regards, Przemek
34
2139
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
3233
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 check on that value. Now I doubt if this is a safe approach and ask for your advice. Thanks a lot in advance. /G
0
9595
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.