Python project: Time Calculator (Cannot seem to get the AM and PM right)

Tell us what’s happening:
It gives me one last failure. It is when the AM are to change into PM. (at 12 noon)
I have been banging my head against this project the whole day, and I am now reaching out for a little bit of help.

So for instance, if I run with “11:59 AM”, “0:01” I obviously want it to change into “PM” but I cannot in my current state figure out how.

My code so far

        def add_time(start, duration, day=None):
    new_time = None

    hr, am_pm = start.split()
    hr, min = start.split(":")
    min = min.split()[0]
    dur_hr, dur_min = duration.split(":")
    hr = int(hr)
    min = int(min)
    dur_hr = int(dur_hr)
    dur_min = int(dur_min)
    weekday = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
    if(am_pm == "PM"):
        hr += 12
    else:
        pass
    new_hr = hr + dur_hr
    new_min = min + dur_min
    while(new_min > 59):
        new_hr += 1
        new_min -= 60

    days_passed = 0
    while(new_hr >23):
        days_passed += 1
        new_hr -= 24

    if(new_hr >= 13 and new_hr%12 != 0):
        am_pm = "PM"
    else:
        am_pm = "AM"


    new_hr = new_hr%12

    if(new_hr%12 == 0):
        if(am_pm == "AM"):
            am_pm = "PM"
        else:
            am_pm = "AM"

    if(new_hr == 0):
        am_pm = "AM"
        new_hr = 12
    else:
        pass

    if(new_min < 10):
        new_min = "0"+str(new_min)
    else:
        pass

    new_time = str(new_hr) + ":" + str(new_min) + " " + am_pm

    if(day):
        new_day = weekday[(weekday.index(day.lower()) + days_passed) % 7].capitalize()
        new_time += ", " + new_day
    if(days_passed == 1):
        new_time += " (next day)"
    if(days_passed > 1):
        new_time += " (" + str(days_passed) + " days later)"
    
    return new_time

INPUT

add_time("11:59 AM", "0:01")

OUTPUT

.....F......
======================================================================
FAIL: test_period_change_at_twelve (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-time-calculator-1/test_module.py", line 25, in test_period_change_at_twelve
    self.assertEqual(actual, expected, 'Expected period to change from AM to PM at 12:00')
AssertionError: '12:05 AM' != '12:05 PM'
- 12:05 AM
?       ^
+ 12:05 PM
?       ^
 : Expected period to change from AM to PM at 12:00

----------------------------------------------------------------------
Ran 12 tests in 0.002s

FAILED (failures=1)

Here is a link to my code:

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Time Calculator

Link to the challenge:

If the while does its job, the new_hr will be less than 23. Then your conditional checks if new_hr is greater than or equal to 13 and divisible by 12. Since new_hr must be between 13 and 23 inclusive, it can’t be divisible by 12, so the condition never matches.

1 Like

Thanks a lot for an informative answer! I have now passed all of the tests.