0

I'm trying to return a tuple from a function but it only returns the first element , When I looked online all the resources said Its done the way I did it , any thoughts ? my code :

def depthFirst(initialState):
    exploredCount = 1
    visitedCount = 0
    frontier=[]
    frontier.append(initialState)
    explored=[]
    while len(frontier) > 0:
        state=frontier.pop()
        explored.append(state)
        visitedCount = visitedCount + 1
        if state in goalStates:
            return state
        nextStates=getNextStates(state)
        for i in range(len(nextStates)):
            if not nextStates[i] in frontier and not nextStates[i] in explored:
                frontier.append(nextStates[i])
                exploredCount = exploredCount + 1

    return [initialState , exploredCount , visitedCount]    

Im callingthe function like this :

    ans = depthFirst(state)
    arr = ans[0]
    explored = arr[1]
    visited = arr[2]
1
  • So what's the initial value of state? What does getNextStates do? Commented Nov 26, 2021 at 14:38

2 Answers 2

1

You're problem is due to the part of the function below:

if state in goalStates:
        return state

The above is ran before the last line of the function is reached. Therefore the function works until the first return (seen above) and only returns state and not the list [initialState , exploredCount , visitedCount]

Sign up to request clarification or add additional context in comments.

1 Comment

That was the issue , stupid hahaha , thanks mate
0

Its returning fine, your first element doesn't have an index of 1

ans = depthFirst(state)

# should work fine
print(ans[0])
print(ans[1])
print(ans[2])

arr = ans[0]
explored = arr[1] # arr doesn't have index 1
visited = arr[2]

EDIT: this is assuming you reach the final return statement and not returning early like the other answer suggest.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.