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

CS50P PSET5:twttr测试本地通过但check50报错求助

CS50P PSET5: check50报错“正确twttr.py无法通过test_twttr检查”的解决方法

问题核心

你本地用pytest跑测试全过,但check50检测时提示「正确的twttr.py无法通过所有test_twttr检查,期望退出码0而非1」,本质原因是你写的测试用例传入了非字符串类型参数,而check50提供的“标准正确版twttr.py”的shorten函数只接受字符串输入,不处理数字(int/float)类型,导致测试时抛出错误,pytest返回非0退出码。

你的twttr.py里特意加了word = str(word)来兼容非字符串输入,所以本地测试没问题,但标准版本没有这个处理逻辑,跑你的测试用例就会报错。

解决步骤

修改test_twttr.py,确保所有传给shorten的参数都是字符串类型——题目要求处理的是文本输入,数字测试应该以字符串形式传入:

修改前的测试片段:

def test_int():
    assert shorten(0) == "0"
    assert shorten(452) == "452"
    assert shorten("21") == "21"

def test_float():
    assert shorten(3.14) == "3.14"
    assert shorten("2.71e") == "2.71"

修改后的测试片段:

def test_int():
    assert shorten("0") == "0"
    assert shorten("452") == "452"
    assert shorten("21") == "21"

def test_float():
    assert shorten("3.14") == "3.14"
    assert shorten("2.71e") == "2.71"

验证

修改完成后,先本地执行pytest test_twttr.py确认所有测试仍通过,再运行check50检测,就能正常通过所有检查项了。

内容的提问来源于stack exchange,提问作者Bhaskar Jha

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 21:52:40