[Python] CS50P-problem set 7-working 9 to 5-pytest exit code

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I don't know why it's showing the exit code for test_working.py is 2. Please see the screenshot for the error message I got from check50: [​IMG]

    Both file works well on my end...

    I ran "pytest test_working.py" on my end and all tests passed

    Can somebody please help me? Thanks!

    Here is the "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()



    Here is the "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")

    Continue reading...

Compartilhe esta Página