473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

small program with strange behavior

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_whi te)
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]
Jul 18 '05 #1
1 1380
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
Jul 18 '05 #2

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

Similar topics

28
by: Robert Gamble | last post by:
I was taking a look at some of the C puzzles at: http://purana.csa.iisc.ernet.in/~gkumar/cquestions.html and have not had any trouble with any of them except for the first one which is reproduced...
1
by: ralphsieminsky | last post by:
I am seeing a strange behavior with a managed/unmanaged C++ program. The program is a large application made of DLLs, COM components, and an exe. When I recompile the exe with /clr a problem...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
8
by: Mike S | last post by:
Hi all, I noticed a very slight logic error in the solution to K&R Exercise 1-22 on the the CLC-Wiki, located at http://www.clc-wiki.net/wiki/KR2_Exercise_1-22 The exercise reads as...
10
by: yaniv.dg | last post by:
hi all, i'm bumping into smething very starnge its very simple code but from some reason the program is acting strange this is my code: #include<stdio.h> #include<conio.h> void main(void) {...
18
by: sam_cit | last post by:
Hi Everyone, int main() { printf("not included stdio.h"); } Yes, i haven't included stdio.h and my compiler would generate a warning and would assume that it would return a int, my question...
169
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide...
73
by: Rajeet Dalawal | last post by:
Good day group. I was asked in an interview to explain the behavior of this program. void main() { char *s = "abc"; int *i = (int *) s; printf("%x", *i); }
9
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.