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.

4

1 回答 1

1

我想是因为这条线

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

你缺少一个右方括号grid_list[grid_list.index(ant_position)[0]

应该是的grid_list[grid_list.index(ant_position)][0],我相信。

编辑正如彼得在评论中建议的那样,将结果存储grid_list.index(ant_position)在一个变量中,并在需要的地方使用该变量。

于 2013-11-08T01:40:07.183 回答