0

I'm trying to write a recursive function in python, but instead of returning an integer type it returns a nonetype. I know there are other questions smiliar to this but not a single solution there worked for me. Here is the code:

class t:
def search(self, globina, board):
    board.legals = board.legal()
    bestMove = 0
    if(globina == 0):
        return board.evaluation()
    elif board.turn == False:
        bestMove = -9999
        for i in range(0, len(board.legals)):
            board.move(board.legals[i])
            board.display()
            bestMove = max(bestMove, self.search(globina-1, board))
            board.undo()
        return bestMove

    else:
        bestMove = 9999
        for i in range(0, len(board.legals)):
            board.move(board.legals[i])
            board.display()
            bestMove = min(bestMove, self.search(globina-1, board))
            board.undo()
        return bestMove
z = t()
z.search(globina, board)     
Annadox
  • 9
  • 2
  • 2
    This code won't even run `IndentationError` – Amit Tripathi Dec 04 '17 at 19:53
  • if globina != 0 it's very likely that you don't return anything – Jean-François Fabre Dec 04 '17 at 19:56
  • @AmitTripathi I think he didn't produce it correctly in the question. Maybe he did do it properly on his editor since he got some output. – Aakash Verma Dec 04 '17 at 19:58
  • @AakashVerma Yes, I think so. But if someone is asking a question SO and expecting others to give their time and help, formatting and readable English is least one can expect. – Amit Tripathi Dec 04 '17 at 20:00
  • @AmitTripathi Look, he's a newbie. Plus I never said that it is convenient for people to read; I said it would have run! – Aakash Verma Dec 04 '17 at 20:01
  • I edited the code, i didn't notice that i didn't even post the whole thing, sorry. – Annadox Dec 04 '17 at 20:02
  • @Annadox: I don't see any way that this code could return `None`, since all of its branches have `return` statements. The only possibility I could imagine is that `board.evaluation()` returns `None` in some situation, but I'd expect that to cause an exception later (since `max(whatever, None)` won't work). Oh, I guess that's a question: Which version of Python are you using? Python 2 does actually allow `None` to be compared to numbers. – Blckknght Dec 04 '17 at 20:31
  • Thanks for the help, board.evaluation() was returning none. – Annadox Dec 04 '17 at 20:39

1 Answers1

0

Your recursive method (as it appears in edited question) is doing the right thing, since it uses return in all of its cases.

That means that the None value must not be due to the recursive function itself, but rather it's a value being returned in the base case somewhere. For your function, that means board.evaluation() returns None at least some of the time.

In Python 3, I'd expect that to cause an exception in any of the recursive cases, as min and max will choke on a None value (since None isn't comparable to anything, including itself). In Python 2 however, all objects are comparable, though there may not be any particular meaning to comparisons between unlike types. The default behavior is to compare class names alphabetically (so all int and float instances are less than all str instances, which are less than all tuples), though if I recall correctly, None compares as less than all other values.

Blckknght
  • 100,903
  • 11
  • 120
  • 169