for d in 'EWNS': if d=='E' and j < len(m[i])-1 and m[i][j+1] != 1: print("E") childCell=(i,j+1) m[i][j+1] = 2 if d=='W' and j>0 and m[i][j-1] != 1: print("W") childCell=(i,j-1) m[i][j-1] = 2 if d=='N' and i>0 and m[i-1][j] != 1: print("N") childCell=(i-1,j) m[i-1][j] = 2 if d=='S' and i
摆脱 for 循环。这是一个不必要的并发症。 def update_scores() temp_g_score=g_score[i][j]+1 print(temp_g_score) temp_f_score=temp_g_score+h(childCell) print(temp_f_score) if j < len(m[i])-1 and m[i][j+1] != 1: print("E") childCell=(i,j+1) m[i][j+1] = 2 update_scores() if j>0 and m[i][j-1] != 1: print("W") childCell=(i,j-1) m[i][j-1] = 2 update_scores() if i>0 and m[i-1][j] != 1: print("N") childCell=(i-1,j) m[i-1][j] = 2 update_scores() if i
像下面这样(我没有你的数据,所以我删除了一些东西): 代码 for d in 'EWNS': if d=='E': print("E") elif d=='W': print("W") elif d=='N': print("N") elif d=='S': print("S") else: print("else") 输出 E W N S 如果我对您的问题的回答令您满意,请考虑接受该答案。 谢谢。

你else只适用于最后一个if,而不适用于前 3if秒。保留第一个if不变,将其他更改为elifs。