期望值“:”Pylance

2024-06-01 04:43:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我找不到丢失的“:”在哪里

我的代码是:

def A_star_Traversal(cost, heuristic, start_point, goals):
    path = []
    hashTable = {}
    for i in goals:
        hashTable[i] = 1
    n = len(cost)
    visited = [False for j in range (n)]
    pq = [[0+heuristic[0],start_point]]
    heapq.heapify(pq)
    while(len(pq)):
        v = heapq.heappop(pq)
        if(hashTable.get(v)):
            path.append(v)
            break
        visited[v] = True
        path.append(v)
        f=1
        for i in range n:     '''this is where i get the error'''
            if(visited[i]==False and cost[v][i]>0):
                pq.append([cost[v][i]+heuristic[i],i])
                heapq.heapify(pq)
                f=0
        if(f):
            path.pop()
    return path

我在for i in range n:处收到错误

我查看了所有for和while循环以及if-else条件语句,确保每个语句都有“:”。然而,尽管没有发现丢失的“:”,但汇编清楚地表明我在某处丢失了它。我希望其他人的情况不一样


Tags: pathinforifrangestartpointheapq
3条回答

您似乎缺少n周围的括号。在Python3中(如果您正在使用),它应该是:

for i in range(n):

试着这样做

for i in range(n):
    #rest of the code

我认为您会出现错误,因为您错过了括号()

the error comes because u didn't enclosed the n in brackets

for i in range(n):

相关问题 更多 >