small program with strange behavior

Discussion in 'Python' started by Dan Stromberg, Nov 22, 2003.

  1. The below small program is giving strange behavior. At the bottom of the code,
    please find "this works" and "this doesn't work" comments. Why does one
    work and the other not?

    TIA.

    #!/usr/bin/python

    import string

    class move_class:
    bad = 'Invalid initializer'

    def __init__(self,str,rank_black,rank_white):
    self.text = str
    self.children = []
    self.rank_black = rank_black
    self.rank_white = rank_white
    if str[0:2] == 'B[':
    #if move_class.prior != '' and move_class.prior != 'W':
    # raise move_class.bad
    if str[1:2] != '[' or str[4:5] != ']':
    raise move_class.bad
    self.letters = string.lower(str[2:4])
    move_class.prior = 'B'
    self.color = 'B'
    self.ranks=[rank_black]
    elif str[0:2] == 'W[':
    #if move_class.prior != 'B':
    # raise move_class.bad
    if str[1:2] != '[' or str[4:5] != ']':
    raise move_class.bad
    self.letters = string.lower(str[2:4])
    #move_class.prior = 'W'
    self.color = 'W'
    self.ranks=[rank_white]
    else:
    raise move_class.bad
    self.strrow = self.letters[0:1]
    self.strcol = self.letters[1:2]
    trow = ord(self.strrow) - ord('a')
    if trow >= 11:
    trow = trow - 1
    self.row = trow
    tcol = ord(self.strcol) - ord('a')
    if tcol >= 11:
    tcol = tcol - 1
    self.col = tcol

    def rotate90(self):
    size=19
    self.row,self.col = self.col,size - self.row - 1

    def copy(self):
    newmove = move_class(self.text,self.rank_black,self.rank_white)
    return newmove

    def __str__(self):
    t = ''
    for i in self.ranks:
    t = t + str(i) + ' '
    return str(self.row)+','+str(self.col)+' '+str(self.color)+' '+\
    t

    move=move_class('B[qp]',-4,-1)

    newgame=[move]

    for r in range(4):
    for move_no in range(0,len(newgame)):
    # this doesn't work
    newmove = newgame[move_no].copy()
    # this works
    #newmove = newgame[move_no]
    newmove.rotate90()
    newgame[move_no]=newmove
    print newgame[move_no]
    Dan Stromberg, Nov 22, 2003
    #1
    1. Advertising

  2. Dan Stromberg

    Peter Hansen Guest

    Dan Stromberg wrote:
    >
    > The below small program is giving strange behavior. At the bottom of the code,
    > please find "this works" and "this doesn't work" comments. Why does one
    > work and the other not?


    You might find people more willing to respond if you (a) shorten your
    not-so-small code down to the smallest size that reproduces the problem
    you are seeing, and (b) define more clearly what "this works" and "this
    doesn't work" actually mean. Are you getting exceptions? Some functionality
    that is not doing the right thing? (If so, maybe tell people what the darn
    thing is supposed to do rather than making them guess.)

    Some people might read all that, execute it, etc, but most of us
    don't have time.

    -Peter
    Peter Hansen, Nov 23, 2003
    #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. Replies:
    0
    Views:
    528
  2. CPERL520335
    Replies:
    1
    Views:
    357
    Sam Holden
    Jul 8, 2003
  3. Ric Nicholls
    Replies:
    2
    Views:
    443
    Greg Schmidt
    Jul 18, 2003
  4. Mantorok Redgormor
    Replies:
    70
    Views:
    1,731
    Dan Pop
    Feb 17, 2004
  5. Guillaume Ebuprofen
    Replies:
    1
    Views:
    102
    Robert Klemme
    Oct 22, 2010
Loading...

Share This Page