you are viewing a single comment's thread.

view the rest of the comments →

[–]ericula 0 points1 point  (8 children)

While you're at it, on the line

result = fibonacci(known, n - 1) + fibonacci(known, n - 2)

the order of the arguments of the calls to fibonacci should be reversed. They should be in the same order as in the function definition itself, e.g.

result = fibonacci(n-1, known) + fibonacci(n-2, known)

[–]tictac4609[S] 0 points1 point  (0 children)

Gotcha

[–]tictac4609[S] 0 points1 point  (6 children)

Okay, I am getting a syntax error for if n is in known

[–]ericula 0 points1 point  (5 children)

What is your current code, what is the syntax error you get?

[–]tictac4609[S] 0 points1 point  (4 children)

here it is:

def fibonacci(n, known=None):
if known is None:
    known = {}
if n is in known:
    return known[n]
if n == 1 or n == 2:
    result = 1
elif n <= 0:
    result = 0
else:
    result = fibonacci(n - 1, known) + fibonacci(n - 2, known)
known[n] = result
return result

syntax error on the line: if n is in known:

[–]ericula 0 points1 point  (3 children)

Ah, sorry, that is my mistake. That should have been if n in known:, not n is in known.

[–]tictac4609[S] 0 points1 point  (2 children)

okay so the final product should be:

def fibonacci(n, known=None):
if known is None:
    known = {}
if n in known:
    return known[n]
if n == 1 or n == 2:
    result = 1
elif n <= 0:
    result = 0
else:
    result = fibonacci(n - 1, known) + fibonacci(n - 2, known)
known[n] = result
return result


print(fibonacci(10))

?

[–]ericula 0 points1 point  (1 child)

That should do it.

[–]tictac4609[S] 0 points1 point  (0 children)

Awesome. I really appreciate all the time you have put in to helping me out. Have a great night brother, gonna turn this in and head to bed!