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