1

When using an embeddded interpreter

>>> import IPython
>>> IPython.embed()

In [1]: def fib(n):
   ...:    if n <= 1:
   ...:        return n
   ...:    else:
   ...:        return fib(n-1) + fib(n-2)
   ...:
In [2]: fib(2)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[2], line 1
----> 1 fib(2)

Cell In[1], line 5, in fib(n)
      3     return n
      4 else:
----> 5     return fib(n-1) + fib(n-2)

NameError: name 'fib' is not defined

Expected output:

In [2]: fib(2)
Out[2]: 1

It gets NameError: name 'fib' is not defined, but the name was defined just before. Why isn't the function functioning?

noob overflow
  • 955
  • 5
  • 14
  • Looks like this has been a known issue since 2010: https://github.com/ipython/ipython/issues/136 but this stackoverflow answer has a workaround: https://stackoverflow.com/a/67517617/6273251 – Random Davis Feb 01 '23 at 19:59

0 Answers0