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 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 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
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 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
Can you please tell me what is the meaning of
UnboundLocalErr or: local variable 'colorIndex' referenced before
assignment
in general? 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
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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
|
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...
|
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
| |
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
|
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
|
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:}
|
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
|
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...
|
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,...
| |
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |