Re: Error:can't assign to operator

Discussion in 'Python' started by Dennis Lee Bieber, Aug 2, 2008.

  1. On Fri, 1 Aug 2008 03:19:25 -0600, "Prasad, Mrunalini"
    <> declaimed the following in
    gmane.comp.python.general:

    > Hello:
    >
    > I am getting the above error while tryign to run the tower of hanoi
    > program. The error is at line 42 highligted in red below. PLease advise
    >

    Note 1: color coding does not get past pure text email/news clients.

    > #!/usr/bin/env python
    >
    > class Hanoi:
    >
    > def __init__(self, N):
    > self.N = N
    >
    > try:
    > raise ValueError(N)
    > except ValueError, e:
    > print "bad! value: %d" % e.value
    >

    Indentation is incorrect here... The unindented try: has ended the
    definition of class Hanoi; as a result, all the indented stuff below is
    seen as part of the except: branch...

    > def display():
    >
    > print "A"
    > for i in range(N):
    > print "%d\n", A
    > i += 1

    What do you expect that i += 1 to be performing? It's just going to
    be replaced by the next value in the range() iteration.

    Also, print statements already include a newline so this would be
    trying to double space the output...

    Moreover, you are printing a literal "%d", a new-line, and the value
    of some global variable named A (which doesn't exist!) on the next line.
    You'd be better off just using:

    for itm in A:
    print itm

    <snip>

    > def move(source, dest):
    >
    > i = 0;j = 0
    > while ((source + i) == 0) and (i < N):


    What is source -- better be some sort of numeric data type...

    This looks more like a badly translated C program which was using
    pointer arithmetic to index through an array given the starting address.

    > dest + j - 1 = source + i


    Especially here... I suspect the C looked something like

    *(dest + j -1) = *(source + i)


    I suggest reading the Python tutorial first before trying to port a
    program from another language.

    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Aug 2, 2008
    #1
    1. Advertising

  2. Dennis Lee Bieber wrote:

    just an fyi....

    > self.towers = [ list(reversed(range(self.numDisks))),


    or range(self.numDisks-1,-1,-1)

    Emile
    Emile van Sebille, Aug 3, 2008
    #2
    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. Rim
    Replies:
    20
    Views:
    703
    Corey Coughlin
    Jul 8, 2003
  2. Replies:
    2
    Views:
    851
    Fredrik Lundh
    Jun 7, 2006
  3. Chris Roth

    Vector Assign vs Vector operator=

    Chris Roth, Feb 21, 2007, in forum: C++
    Replies:
    4
    Views:
    367
    Ron Natalie
    Feb 22, 2007
  4. Replies:
    2
    Views:
    2,220
    John Harrison
    May 26, 2007
  5. Emile van Sebille

    Re: Error:can't assign to operator

    Emile van Sebille, Aug 1, 2008, in forum: Python
    Replies:
    1
    Views:
    465
Loading...

Share This Page