[Tutor] Error
Alan Gauld
alan.gauld at btinternet.com
Tue Apr 22 02:08:59 CEST 2014
On 21/04/14 19:41, Geocrafter . wrote:
> im trying to make a board, and is detecting the pieces. Here is my
> code:http://pastebin.com/L3tQLV2g And here is the error:
> http://pastebin.com/4FiJmywL Do you knwo hwo to fix it?
You are passing in a cell location that results in an index out of range.
Try figuring out what happens when x is 6 for example.
BTW Cant you combine those two enormous if statements into one by just
passing ionm the test character(x or y) as an parameter?
Like this:
def check_around(x, y, test='x'):
if test == 'x': check = 'o'
elif test == 'o': check = 'x'
else: raise ValueError
if board[x][y] == test:
if board[x - 3][y - 3] == check or board[x - 2][y - 3] == check
or board[x - 1][y - 3] == check or...
However even better would be to get rid of the huge if statement and
replace it with loops to generate the indices. Pythons range function
can deal with negatives too:
Try:
>>> print list(range(-3,4))
to see if that gives you any ideas.
Finally your mqain code doesn't appear to do anything very much...
for x in range (0, 7):
for y in range (0, 7):
check_aroundx(x, y)
This only checks the x cells. It would be better practice
to have the function return a result that you can print
externally. Maybe return a list of touching cells say?
# sys.stdout.write ("%c" % (board[x][y]))
print
And these two lines don't do anything much.
Just some thoughts.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list