我无法弄清楚错误的来源,因为据我所知,代码是正确的并且没有明显的错误。
代码:
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")
错误可能发生在if语句末尾冒号上的print(“gridsize,gridsize”)正上方的行上。我不知道问题是什么。任何帮助表示赞赏。
答案 0 :(得分: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]
。
编辑正如Peter在评论中所建议的那样,将grid_list.index(ant_position)
的结果存储在变量中,并在需要的地方使用变量。