user28845228
user28845228

Reputation: 11

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

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: error message i got

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")

Upvotes: 1

Views: 123

Answers (1)

Catherine Rivas
Catherine Rivas

Reputation: 61

I had the same problem (my logs were identical), I spent almost 15 days without being able to get this problem through the CS50 check, it turns out that in the end the check looks for the convert() function to do the tests so I changed the name of my original function from "formart_verifier" to "convert" and only used that function in my test file (not one of the ones I created) and that did the trick. It only took me 2 weeks to figure it out.

Upvotes: 0

Related Questions