0

Can someone help me? I've been stuck on this one for ages, not sure what went wrong and I don't know how to get rid of the exit code 1, not 0 part

import re
import sys

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


def convert(s):
    if matches := re.search(r"^(([0-1]?[0-9]):?[0-5]?[0-9]? (A|P)M) to (([0-1]?[0-9]):?[0-5]?[0-9]? (A|P)M)$",s, re.IGNORECASE):
        time1 = matches.group(1)
        time2 = matches.group(4)
        if int(matches.group(2)) > 12 or int(matches.group(5)) > 12:
            raise ValueError
        return (f"{conversion(time1)} to {conversion(time2)}")
    else:
        raise ValueError

def conversion(prompt_number):
    number, time_period = prompt_number.split(" ")
    if "AM" in prompt_number:
        if ":" in number:
            hrs, mins = number.split(":")
            if int(hrs) == 12:
                return(f"00:00")
            elif int(hrs) < 10:
                return(f"0{hrs}:{mins}")
        elif int(number) == 12:
            return(f"00:00")
        else:
            if int(number) < 10:
                return(f"0{number}:00")
            else:
                return(f"{number}:00")

    if "PM" in prompt_number:
        if ":" in number:
            hrs, mins = number.split(":")
            if int(hrs) == 12:
                return (f"{number}")
            else:
                new_hour = int(hrs) + 12
                return (f"{new_hour}:{mins}")

        elif int(number) == 12:
            return (f"{number}:00")
        else:
            new_hour = int(number) + 12
            return (f"{new_hour}:00")


if __name__ == "__main__":
    main()

Test

from working import convert
import pytest

def test_convert():
    assert convert("12 PM to 8 AM") == "12:00 to 08:00"
    assert convert("12 AM to 8 AM") == "00:00 to 08:00"
    assert convert("5 PM to 11 PM") == "17:00 to 23:00"
    assert convert("1 AM to 10 AM") == "01:00 to 10:00"
    assert convert("4:30 AM to 5:15 PM") == "04:30 to 17:15"
    assert convert("3:08 PM to 9:00 PM") == "15:08 to 21:00"


def wrong_test_convert():
    with pytest.raises(ValueError):
        convert("19:00 AM to 20:00 PM")
        convert("14 AM to 9 PM")
        convert("3:60 AM to 10:90 PM")
        convert("6 AM - 4 PM")
        convert("06:00 AM - 16:00 PM")
        convert("11:00AM to 4:00PM")

enter image description here

1 Answer 1

2

First, some background. The 2nd set of tests are running YOUR test_working.py against THE COURSE working.py (to "test the test"). THE COURSE working.py has (intentional) known errors and YOUR test_working.py should catch them. It's a gap in test coverage if it doesn't.

There are a 2 subtle issues to address:

  1. The name of the second function needs to start with "test_". For example, modify from wrong_test_convert() to test_convert_wrong().
  2. Next, each ValueError test needs a separate with statement. Like this:
    def test_convert_wrong():
        with pytest.raises(ValueError):
            convert("19:00 AM to 20:00 PM")
        with pytest.raises(ValueError):
            convert("14 AM to 9 PM")

Fix those 2 items and your test_working.py code should pass the last 2 tests.

2
  • Thank you!!! So much!!!!
    – Lucas
    Commented Sep 3 at 1:40
  • 1
    Glad to help. If this solved your problem, please use the check to "Accept Answer" so others will know this is a valid solution. Good luck with CS50P.
    – kcw78
    Commented Sep 4 at 1:35

You must log in to answer this question.

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