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

CS50P Problem Set7 作业9 to 5:本地pytest全通过但check50返回exit code 2求助

CS50P Problem Set7 作业9 to 5:本地pytest全通过但check50返回exit code 2求助

我碰到个头疼的问题:本地运行pytest test_working.py时所有测试都能顺利通过,但用check50检测时,test_working.py居然返回了退出码2,附上我拿到的错误截图:

error message i got

两个文件在我本地跑起来都没问题,实在搞不懂哪里出问题了,有没有大佬能帮我排查下?万分感谢!

下面是我的working.py代码:

import re
import sys


def main():
    try:
        print(convert(input("Hours: ").strip()))
        sys.exit(0)
    except ValueError as e:
        print(e)
        sys.exit(1)



def convert(s):
    matches = re.search(r"^(1?[0-9]):?([0-6][0-9])? (AM|PM) to " \
                    r"(1?[0-9]):?([0-6][0-9])? (AM|PM)$", s)
    if not matches:
        raise ValueError("ValueError")
    else:
        from_hour, from_min = matches.group(1), matches.group(2)
        from_meridiem = matches.group(3)
        to_hour, to_min = matches.group(4), matches.group(5)
        to_meridiem = matches.group(6)

        from_hour = convert_hour(from_hour, from_meridiem)
        to_hour = convert_hour(to_hour, to_meridiem)

        from_min = convert_min(from_min)
        to_min = convert_min(to_min)

        if ((from_hour == None) or (from_min == None) or
            (from_hour == None) or (from_min == None)):
            raise ValueError("ValueError")

        return f"{from_hour}:{from_min} to {to_hour}:{to_min}"



def convert_hour(h, meridiem):
    if 1 <= int(h) <= 12:
        if meridiem == "AM":
            if len(h) == 1:
                return ("0"+ h)
            elif h == "12":
                return "00"
            else:
                return h
        else:
            if h == "12":
                return h
            else:
                return f"{int(h) + 12}"
    else:
        return None



def convert_min(min):
    if min == None:
        return "00"
    elif 0 <= int(min) <= 59:
        return min
    else:
        return None



if __name__ == "__main__":
    main()

以及test_working.py代码:

import pytest
from working import convert, convert_hour, convert_min


def test_convert():
    assert convert("9 AM to 5 PM") == "09:00 to 17:00"
    assert convert("9:00 AM to 5:00 PM") == "09:00 to 17:00"
    assert convert("10 AM to 8:50 PM") == "10:00 to 20:50"
    assert convert("10:30 PM to 8 AM") == "22:30 to 08:00"


def test_convert_hour():
    assert convert_hour("9", "AM") == "09"
    assert convert_hour("12", "AM") == "00"
    assert convert_hour("12", "PM") == "12"
    assert convert_hour("1", "PM") == "13"
    assert convert_hour("13", "PM") == None
    assert convert_hour("0", "PM") == None
    assert convert_hour("0", "AM") == None
    assert convert_hour("13", "AM") == None


def test_convert_min():
    assert convert_min("60") == None
    assert convert_min("30") == "30"


def test_value_error():
    with pytest.raises(ValueError):
        convert("14:50 AM to 13:30 PM")
    with pytest.raises(ValueError):
        convert("9:60 AM to 5:60 PM")
    with pytest.raises(ValueError):
        convert("9 AM - 5 PM")
    with pytest.raises(ValueError):
        convert("09:00 AM - 17:00 PM")

备注:内容来源于stack exchange,提问作者user28845228

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 18:18:02