更改字符串参数值的方法(Python)

2024-09-29 21:31:30 发布

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

上周,我开始在EdX学习一门人工智能在线课程,我一直在努力推进这个项目,但我还没能修复我遇到的一个特定错误。这个项目包括建立一个A*代理,它可以在吃豆人迷宫中找到所有的4个角落。你知道吗

课程论坛目前不是很活跃,我在Stackoverflow上也没有发现类似的东西,所以我决定问问自己。我正在编写的代码是本课程构建的更大项目的一部分,解决迷宫问题的类是CornersProblem,它有一个构造函数、getStartState、isCorner、isGoalState、getCostOfActions、cornersHeuristic和getsuccessivers方法。你知道吗

引发错误的代码如下:

    def getSuccessors(self, state):
    """
    Returns successor states, the actions they require, and a cost of 1.
     As noted in search.py:
        For a given state, this should return a list of triples, (successor,
        action, stepCost), where 'successor' is a successor to the current
        state, 'action' is the action required to get there, and 'stepCost'
        is the incremental cost of expanding to that successor
    """
    successors = []
    self._expanded += 1  # DO NOT CHANGE
    for direction in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]:
        x, y = state[0]
        **print direction
        dx, dy = Actions.directionToVector(direction)**
        nextx, nexty = int(x + dx), int(y + dy)
        if not self.walls[nextx][nexty]:
            nextState = (nextx, nexty)
            cost = self.getCostOfActions(direction)
            if self.isCorner(self, nextState):
                state[1] += 1
            successors.append(((nextState, state[1]), direction, cost))
    return successors

此方法调用directionToVector:

    def directionToVector(direction, speed = 1.0):
    print direction
    dx, dy =  Actions._directions[direction]
    return (dx * speed, dy * speed)
directionToVector = staticmethod(directionToVector)

我得到了一个KeyError消息:“N”表示“N”不在字典中,应该是“North”。如果我在将方向传递给directionToVector方法之前打印它,它会输出'North',但在它位于方法内部之后,输出是'N'。错误信息如下:

Command line error

我对Python不是很有经验,所以这里可能缺少一些东西。这是我在这个论坛上的第一个问题,所以提前谢谢你的回答。你知道吗


Tags: ofthe项目方法selfreturn课程state

热门问题