You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行doctest时提示“Got nothing”的含义及BST方法问题排查

问题分析与解决

核心问题原因

你遇到的“Got nothing”提示,本质是doctest没有捕获到预期的输出内容,结合代码来看有两个关键问题:

  1. 方法行为与测试用例不匹配
    测试用例期望调用bst.best_apartment()后直接输出PRIVATE: 80,但你的方法只是返回了公寓对象,并没有打印任何内容。doctest会检查控制台输出,没有输出自然会提示“Got nothing”。

  2. 返回类型标注错误
    方法声明-> None,表示该方法应该返回None,但你实际返回了current_node.apartment,这属于类型不匹配,虽然不会直接导致doctest失败,但不符合代码规范。

修复方案(二选一即可)

方案1:修改测试用例,捕获返回值并打印

保持方法的返回逻辑不变,调整doctest里的调用方式,显式打印返回的公寓对象:

def best_apartment(self):
    """Return (one of) the apartment(s) with the best evaluation
    score in the BSTTree.
    >>> apartments = read_apartment_data("apartments.csv")[0:300]
    >>> bst = BSTTree()
    >>> bst.build_tree(apartments)
    >>> print(bst.best_apartment())
    PRIVATE: 80
    """

    if self.is_empty():
        return None

    current_node = self
    while current_node.right is not None:
        current_node = current_node.right

    return current_node.apartment

方案2:修改方法,改为打印结果而非返回

如果测试用例的要求是直接输出结果,那么调整方法逻辑,让它打印公寓信息而不是返回:

def best_apartment(self) -> None:
    """Print (one of) the apartment(s) with the best evaluation
    score in the BSTTree.
    >>> apartments = read_apartment_data("apartments.csv")[0:300]
    >>> bst = BSTTree()
    >>> bst.build_tree(apartments)
    >>> bst.best_apartment()
    PRIVATE: 80
    """

    if self.is_empty():
        return

    current_node = self
    while current_node.right is not None:
        current_node = current_node.right

    print(current_node.apartment)

额外说明

你找最高评分公寓的BST遍历逻辑是正确的——二叉搜索树中,键值最大的节点(对应最高评分,假设BST按评分升序构建)确实在最右侧分支的末端,这部分无需修改。

内容的提问来源于stack exchange,提问作者Kian Power

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 03:32:45