0

I cannot figure out where the error is coming from because as far as i can see the code is correct and has no obvious errors in it.

code:

if grid_list[grid_list.index(ant_position)][0] == 1 or grid_list[grid_list.index(ant_position)][1] == 1:
 print("boom")
 if grid_list[grid_list.index(ant_position)][1] ==1 and grid_list[grid_list.index(ant_position)][0] == 1:
 print("1,1")
 else:
 if grid_list[grid_list.index(ant_position)][1] == grid_size or grid_list[grid_list.index(ant_position)][0] == grid_size:
 if grid_list[grid_list.index(ant_position)][0] == grid_size:
 print("gridsize,1")
 else:
 print("1,gridsize")
 else:
 if grid_list[grid_list.index(ant_position)][0] == 1:
 print("1,something")
 else:
 print("something,1")
else:
 if grid_list[grid_list.index(ant_position)[0] == grid_size or grid_list[grid_list.index(ant_position)][1] == grid_size:
 print("boom")
 if grid_list[grid_list.index(ant_position)][1] == grid_size and grid_list[grid_list.index(ant_position)][0] == grid_size:
 print("gridsize,gridsize")
 else:
 if grid_list[grid_list.index(ant_position)][0] == grid_size:
 print("gridsize,something")
 else:
 print("something,gridsize")
 else: 
 print("boo")

The error is supposedly occuring on the line just above the print("gridsize,gridsize") on the colon at the end of the if statement. I have no idea what the problem is. any helps appreciated.

asked Nov 8, 2013 at 1:38
0

1 Answer 1

1

I suppose it is because of this line

if grid_list[grid_list.index(ant_position)[0] == grid_size or grid_list[grid_list.index(ant_position)][1] == grid_size:

you are missing a closing square bracket in grid_list[grid_list.index(ant_position)[0]

It should have been grid_list[grid_list.index(ant_position)][0], I believe.

Edit As Peter suggested in the comments, store the result of grid_list.index(ant_position) in a variable and use the variable wherever needed.

answered Nov 8, 2013 at 1:40
Sign up to request clarification or add additional context in comments.

5 Comments

For the sake of readability and performance, stop calling grid_list.index(ant_position) everywhere. Call it once and keep the value.
@PeterDeGlopper Thats true. I ll add your suggestion in the answer.
I suspect there are other ways this code could be cleaned up, but that's an easy one. Actually, now this hurts my head - the OP constantly looks up grid_list[grid_list.index(ant_position)]. But isn't that just a slow and cumbersome way to get ant_position? Assuming grid_list is a normal sequence type and not something with a custom index, anyway.
yeah I spotted it about 1 minute after i posted the question :3 i was looking in the wrong place because of where the syntax error was highlighting :L thankyou very much though and yeah that is a good idea but it would require constant iterations of a function ive written so i probabaly wont bother :L
ahhhh thank you very much for pointing out the grid_list thing :L that really shortens down my code :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.