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

问题描述 投票:0回答:2

我是 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

我在这里做错了什么?我是否缺少某些内容来查看文档字符串?

python-3.x
2个回答
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

0
投票

NameError Traceback(最近一次调用最后一次) 在 () 中 6 print("结果:");print('*'50);print("已配置 URL 和更新请求类型!");print(''*50); 7 ----> 8 响应,内容 = http.request(ENDPOINT,method="POST",body=content) 9 输出 = 响应['状态'] 10

名称错误:名称“http”未定义

请帮我查一下

© www.soinside.com 2019 - 2024. All rights reserved.