2010-07-14 85 views
2

當我執行它時,它給我一個錯誤,即太多的值解壓縮? 我如何使它正常工作。值錯誤:太多的值解壓

stack = util.Stack() 
    closed = [] 
    child = [] 
    index = 0 
    currNode = problem.getStartState() 
    node = currNode 
    stack.push(node) 
    while not stack.isEmpty(): 
    node = stack.pop() 
    if problem.isGoalState(node): 
     print "true" 
     closed.append(node) 
    else: 
     child = problem.getSuccessors(node) 
     for nodes in child: 
      stack.push(nodes) 
     closed.append(node) 
    return None  

錯誤是:

File line 90, in depthFirstSearch 
    child = problem.getSuccessors(node) 
    File line 179, in getSuccessors 
    x,y = state 
**ValueError: too many values to unpack** 

爲getsuccessor FUNC的代碼是:

def getSuccessors(self, state): 
    """ 
    Returns successor states, the actions they require, and a cost of 1. 

    """ 

    successors = [] 
    for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]: 
     x,y = state 
     dx, dy = Actions.directionToVector(action) 
     nextx, nexty = int(x + dx), int(y + dy) 
     if not self.walls[nextx][nexty]: 
     nextState = (nextx, nexty) 
     cost = self.costFn(nextState) 
     successors.append((nextState, action, cost)) 

返回的值該功能最初:

problem.getStartState() - (5, 5) 
problem.isGoalState(problem.getStartState())- False 
problem.getSuccessors(problem.getStartState()) - [((5, 4), 'South', 1), ((4, 5), 'West', 1)] 
+0

如果有人需要更多的信息,請讓我知道。 – Shilpa 2010-07-14 22:48:48

+1

@Shilpa:錯誤發生在179行,所以你應該在那裏發佈代碼。 – sth 2010-07-14 22:52:28

+0

179行是 x,y =州 我編輯了我的問題。請參閱getSuccessor函數。 – Shilpa 2010-07-14 22:57:31

回答

4

首先,它是不太可能是w洞getSuccessors方法,因爲沒有返回值。

猜測,我會說getSuccessors返回元組列表:(nextState,action,cost)。你將每個節點都存儲爲節點,當你將一個節點傳回方法時會失敗,並嘗試將這三個值解包爲兩個值。

你應該找到一個體面的調試器,並學習如何使用它。我使用Eclipse(與PyDev),它將顯着幫助你解決這些類型的錯誤。

+0

我編輯了這個問題。現在你可以看到getsuccessor函數。 179行是x,y =州 – Shilpa 2010-07-14 22:58:09

+0

此外我不能更改此代碼。我可以改變我自己的代碼在頂部。 – Shilpa 2010-07-14 22:58:42

+0

是的...你是絕對正確的。但我不能想到改變我的代碼。我如何在我的代碼中進行更改。 – Shilpa 2010-07-14 23:31:20

0

不知道爲什麼有不一致的大小元組傳遞到getSuccessors,但您可以通過檢查node = stack.pop()行之後的node長度來解決。如果它是3,那麼你會想通過node[0]在行child = problem.getSuccessors(node)

+0

節點只是一個存儲(5,5)的初始節點值的變量。我如何檢查長度....打印len(節點)? – Shilpa 2010-07-14 23:21:52

+0

我需要返回達到目標的操作列表。那麼我應該回報什麼價值?我不能在while循環結束後使用return None。 – Shilpa 2010-07-15 00:26:38

相關問題