0

I was wondering what causes the check50 final test case to yield an error. The error message is as follows:

:( test_working.py catches working.py not raising ValueError for invalid time format

expected exit code 1, not 0

Here is my working.py:

import re
import sys


def main():
    print(convert(input("Hours: ")))

def convert(s):
    try:
        matches = re.search(r"^(\d{1,2})( |:\d{2} )(AM|PM) to (\d{1,2})( |:\d{2} )(AM|PM)$", s)
        hour1, minute1, time1, hour2, minute2, time2 = matches.group(1), matches.group(2), matches.group(3), matches.group(4), matches.group(5), matches.group(6)
        if hour1[0] == "0" or hour2[0] == "0":
            raise ValueError

        if minute1 == " ":
            minute1, minute2 = "00", "00"
        else:
            minute1, minute2 = minute1[1:3], minute2[1:3]

        if 1 <= int(hour1) <= 12 and 1 <= int(hour2) <= 12 and 0 <= int(minute1) <= 59 and 0 <= int(minute2) <= 59:
            match time1:
                case "AM":
                    xHour1 = int(hour1) % 12
                case "PM":
                    xHour1 = (int(hour1) % 12 + 12)
            match time2:
                case "AM":
                    xHour2 = int(hour2) % 12
                case "PM":
                    xHour2 = (int(hour2) % 12 + 12)
            return (f"{xHour1:02}:{minute1} to {xHour2:02}:{minute2}")
        else:
            raise ValueError
    except:
        raise ValueError

if __name__ == "__main__":
    main()

Here is my test_working.py:

from working import convert
import pytest

def test_invalidRange():
    with pytest.raises(ValueError):
        convert("9:60 AM to 5:60 PM")

def test_invalidHourMinute():
    with pytest.raises(ValueError):
        convert("13:00 AM to 00:60 PM")
        convert("09:00 AM to 12:00 PM")

def test_invalidSeperator():
    with pytest.raises(ValueError):
        convert("9:00 AM - 5:30 PM")

def test_noMinutes():
    with pytest.raises(ValueError):
        convert("9: AM - 5: PM")

def test_12AM12PM():
    assert convert("12 AM to 12 PM") == "00:00 to 12:00"
    assert convert("12:00 AM to 12:00 PM") == "00:00 to 12:00"

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

Here is my check50 result: https://submit.cs50.io/check50/65fc850a3e906934be3e50086924b9a98c1d4840

Any inputs on what I have done wrongly here? Thanks!

1 Answer 1

2

Debugging the test_ functions can be a challenge. The error message only hints at what pytest should be testing (small understatement). Some background: the pytest tests of test_working.py are checking your test code with their working.py (not yours).

I'll give you a hint (without telling you exactly what to do). Look at the tests on working.py. You will find one that looks for missing AM/PM values. Your code working.py handles it correctly (raises ValueError). See below:

:) working.py raises ValueError when given "09:00 to 17:00"

Now, look at your test_working.py. Do you have a check for AM/PM? Fix that, and it should pass check50. :-)

1
  • Ah right! Thanks so much :)
    – dkevin77
    Oct 11, 2022 at 16:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .