How can I find the remainder when dividing 2 integers

Discussion in 'Python' started by silverburgh.meryl@gmail.com, Feb 26, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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
     
    Diez B. Roggisch, Feb 26, 2006
    #2
    1. Advertising

  3. David Guest

    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
     
    David, Feb 26, 2006
    #3
  4. Guest

    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, 2006
    #4
  5. 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
     
    Diez B. Roggisch, Feb 26, 2006
    #5
  6. Guest

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

    in general?
     
    , Feb 26, 2006
    #6
  7. 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
     
    Diez B. Roggisch, Feb 26, 2006
    #7
  8. Kent Johnson Guest

    Diez B. Roggisch wrote:
    > 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
     
    Kent Johnson, Feb 26, 2006
    #8
  9. On 26 Feb 2006 12:07:53 -0800, declaimed the
    following in comp.lang.python:

    > 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)
    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Feb 26, 2006
    #9
  10. On Sun, 26 Feb 2006 21:58:30 +0100, Diez B. Roggisch wrote:

    > 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.
     
    Steven D'Aprano, Feb 26, 2006
    #10
  11. 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
     
    Andrea Griffini, Feb 26, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manfred Balik

    polynomial division remainder

    Manfred Balik, May 12, 2004, in forum: VHDL
    Replies:
    5
    Views:
    3,360
    Mike Treseler
    May 18, 2004
  2. John Saunders

    Re: Remainder

    John Saunders, Jun 28, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    414
    John Saunders
    Jun 28, 2004
  3. =?Utf-8?B?Sm9obiBCYW5raGVhZA==?=

    Table Column Width - Setting as remainder of page

    =?Utf-8?B?Sm9obiBCYW5raGVhZA==?=, Mar 24, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    505
    =?Utf-8?B?Sm9obiBCYW5raGVhZA==?=
    Mar 24, 2005
  4. Replies:
    11
    Views:
    747
  5. Efficient division/remainder in C

    , Jan 13, 2006, in forum: C Programming
    Replies:
    29
    Views:
    2,319
    Michael Wojcik
    Jan 20, 2006
Loading...

Share This Page