I'm curious why this first function does work (OOP) but the second triggers and error. Can someone explain?
class Solution(object):
def __init__(self):
self.solved = {0:0,1:1,2:1}
def fibonacci(self, n):
"""
:type n: int
:rtype: int
"""
if n in self.solved.keys():
return self.solved[n]
else:
value = self.fibonacci(n-1) + self.fibonacci(n-2)
self.solved[n] = value
return value
t = Solution()
t.fibonacci(7)
>>> 13
But this doesn't work
found = {0:0,1:1,2:1}
def fibonacci(n):
if n in found.keys():
return found[n]
else:
value = fibonacci(n-1) + fibonacci(n-2)
found[n] = value
return value
fibonacci(7)
>>>TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Edit: Error only found when executed on Google Colab, not in local python IDE.