回溯(最近一次调用最后一次)-Python 中的 NameError

时间:2021-06-23 14:28:30

标签: python-3.x

我是 Python 新手,正在通过 Kaggle 教程学习。在其中一个功能部分中,我正在输入此代码。

def least_difference(a, b, c):
    """
    Return the smallest difference between any two numbers among a, b, & c

    """
    diff1 = abs(a - b)
    diff2 = abs(b - c)
    diff3 = abs(a - c)
    return min(diff1, diff2, diff3)


print(
    least_difference( 2, 200, 100 ),
    least_difference( 1, 10, 10 ),
    least_difference(5, 6, 7),
)

本教程围绕使用帮助功能展开。所以当我输入

>>> help(least_difference)

我收到错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'least_difference' is not defined

我在这里做错了什么?看文档字符串有什么我遗漏的吗?

1 个答案:

答案 0 :(得分:1)

您需要将文件加载到交互式上下文中。使用:

$ python -i ./relative/path/to/file.py # or python3 depending on your PATH
...
>>> help(least_difference)
Return the smallest difference between any two numbers among a, b, & c

或者,直接从 REPL 导入:

$ python # or python3 depending on your PATH
...
>>> import ./relative/path/to/file.py
>>> help(least_difference)
Return the smallest difference between any two numbers among a, b, & c
相关问题