1

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

5
  • Are you sure that is the actual content of test_working.py? Are you sure you saved it? Commented Dec 18, 2024 at 21:56
  • Yes, I'm sure, I tried a couple of times both yesterday and today Commented Dec 18, 2024 at 23:16
  • According to the course documentation, the check50 program is supposed to include a url in its output you can visit to see the failure results in more detail. Did you visit that url? Commented Dec 18, 2024 at 23:26
  • 1
    I kind of solved it by removing other "help functions" (convert_hour, convert_min) and just using the convert function in working.py. It works. BUT, I don't quite understand.. the problem set itself says we can implement other functions. Also, this is making my code super redundant.. Commented Dec 18, 2024 at 23:46
  • 1
    Here's a guess why your solution worked -- I suspect check50 doesn't isn't aware of your helper functions and only uses from working import convert. Then you get an error when they are called. You can verify this by making convert_hour() and convert_min() as inner functions of convert(). Then they will be included with the imported function. Modify your pytest code appropriately after making these changes. Commented Dec 27, 2024 at 23:32

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.