[1]:
%reload_ext jupyter_ai

[2]:
2/2

[2]:
1.0
[3]:
2/0

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[3], line 1
----> 1 2/0

ZeroDivisionError: division by zero
[4]:
%%ai chatgpt
Explain the following Python error:
    {Err[3]}

[4]:

The error is a ZeroDivisionError. It occurred because the code attempted to divide a number by zero, which is mathematically undefined. This error message specifically states “division by zero”, indicating that there was an attempt to divide by zero.


您还可以使用 %ai error 命令,该命令使用与基本 %%ai 命令相同的参数,用于解释最近的错误。

[5]:
print 'foo'

  Cell In[5], line 1
    print 'foo'
    ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

[6]:
print(Err[5])

  Cell In[5], line 1
    print 'foo'
    ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

[7]:
%ai error chatgpt

[7]:

The error is a SyntaxError. It occurred because the code uses Python 2 syntax to print a string without parentheses, but the notebook is running in Python 3, which requires parentheses for print statements.

The error message specifies that the issue is “Missing parentheses in call to ‘print’”. The message also suggests a possible solution by stating “Did you mean print(…)?” which indicates that enclosing the string “foo” in parentheses will resolve the syntax error.

[8]:
%%ai chatgpt --format code
The following Python code:
--
{In[5]}
--
produced the following Python error:
--
{Err[5]}
--
Write a new version of this code that does not produce that error.

[8]:
AI generated code inserted below ⬇️
[ ]:
print('foo')

[ ]: