Code isnt working properly

This is my code an error message. Please help

def add_time(start, duration, today=None):
    # variables
    startTime, startEnding = start.split()
    startHour, startMinute = startTime.split(":")

    durationHour, durationMinute = duration.split(":")

    week = [
        "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
        "Sunday"
    ]
    index = 0

    #getting index of today
    if today != None:
        for day in week:
            if day.lower() == today.lower():
                break
            else:
                index += 1

    newMinute = int(startMinute) + int(durationMinute)
    newHour = int(startHour) + int(durationHour)

    # convert minutes to the right format
    floor, mod = divmod(newMinute, 60)
    if mod <= 9:
        newMinute = "0" + str(mod)
        newHour += floor
    else:
        newMinute = str(mod)

    # convert hours to the right format
    floor, mod = divmod(newHour, 12)
    if mod == 0:
        newHour = 12
    else:
        newHour = mod

    #get the new ending
    if startEnding == "AM":
        if floor % 2 == 0:
            newEnding = "AM"
        else:
            newEnding = "PM"
    else:
        if floor % 2 == 0:
            newEnding = "PM"
        else:
            newEnding = "AM"

    #get the end day
    if startEnding == "AM":
        index += int(floor / 2)
    else:
        index += int((floor + 1) / 2)

    #get days passed
    if index == 1:
        daysPassed = "(next day)"
    elif index != 0:
        daysPassed = "(" + str(index) + " days later)"

    if index == 0:
        if today == None:
            newTime = str(newHour) + ":" + newMinute + " " + newEnding
        else:
            newTime = str(
                newHour) + ":" + newMinute + " " + newEnding + ", " + week[
                    index % 7]
    else:
        if today == None:
            newTime = str(
                newHour) + ":" + newMinute + " " + newEnding + " " + daysPassed
        else:
            newTime = str(
                newHour) + ":" + newMinute + " " + newEnding + ", " + week[
                    index % 7] + " " + daysPassed

    return newTime

12:04 AM (next day)
..F......F.F
======================================================================
FAIL: test_high_duration_with_day (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-time-calculator-2/test_module.py", line 65, in test_high_duration_with_day
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "8:16 PM", "466:02", "tuesday" to return "6:18 AM, Monday (20 days later)"')
AssertionError: '6:18 AM, Monday (21 days later)' != '6:18 AM, Monday (20 days later)'
- 6:18 AM, Monday (21 days later)
?                   ^
+ 6:18 AM, Monday (20 days later)
?                   ^
 : Expected calling "add_time()" with "8:16 PM", "466:02", "tuesday" to return "6:18 AM, Monday (20 days later)"

======================================================================
FAIL: test_twenty_four_with_day (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-time-calculator-2/test_module.py", line 55, in test_twenty_four_with_day
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "2:59 AM", "24:00", "saturDay" to return "2:59 AM, Sunday (next day)"')
AssertionError: '2:59 AM, Sunday (6 days later)' != '2:59 AM, Sunday (next day)'
- 2:59 AM, Sunday (6 days later)
 python main.py
12:04 AM (next day)
..F......F.F
======================================================================
FAIL: test_high_duration_with_day (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-time-calculator-2/test_module.py", line 65, in test_high_duration_with_day
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "8:16 PM", "466:02", "tuesday" to return "6:18 AM, Monday (20 days later)"')
AssertionError: '6:18 AM, Monday (21 days later)' != '6:18 AM, Monday (20 days later)'
- 6:18 AM, Monday (21 days later)
?                   ^
+ 6:18 AM, Monday (20 days later)
?                   ^
 : Expected calling "add_time()" with "8:16 PM", "466:02", "tuesday" to return "6:18 AM, Monday (20 days later)"

======================================================================
FAIL: test_twenty_four_with_day (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-time-calculator-2/test_module.py", line 55, in test_twenty_four_with_day
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "2:59 AM", "24:00", "saturDay" to return "2:59 AM, Sunday (next day)"')
AssertionError: '2:59 AM, Sunday (6 days later)' != '2:59 AM, Sunday (next day)'
- 2:59 AM, Sunday (6 days later)
 python main.py
12:04 AM (next day)
..F......F.F
======================================================================
FAIL: test_high_duration_with_day (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-time-calculator-2/test_module.py", line 65, in test_high_duration_with_day
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "8:16 PM", "466:02", "tuesday" to return "6:18 AM, Monday (20 days later)"')
AssertionError: '6:18 AM, Monday (21 days later)' != '6:18 AM, Monday (20 days later)'
- 6:18 AM, Monday (21 days later)
?                   ^
+ 6:18 AM, Monday (20 days later)
?                   ^
 : Expected calling "add_time()" with "8:16 PM", "466:02", "tuesday" to return "6:18 AM, Monday (20 days later)"

======================================================================
FAIL: test_twenty_four_with_day (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-time-calculator-2/test_module.py", line 55, in test_twenty_four_with_day
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "2:59 AM", "24:00", "saturDay" to return "2:59 AM, Sunday (next day)"')
AssertionError: '2:59 AM, Sunday (6 days later)' != '2:59 AM, Sunday (next day)'
- 2:59 AM, Sunday (6 days later)
?                  ^    -------
+ 2:59 AM, Sunday (next day)
?                  ^^^^
 : Expected calling "add_time()" with "2:59 AM", "24:00", "saturDay" to return "2:59 AM, Sunday (next day)"

======================================================================
FAIL: test_two_days_later_with_day (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-time-calculator-2/test_module.py", line 60, in test_two_days_later_with_day
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "2:59 AM", "24:00", "Friday" to return "12:04 AM, Friday (2 days later)"')
AssertionError: '12:04 AM, Friday (4 days later)' != '12:04 AM, Friday (2 days later)'
- 12:04 AM, Friday (4 days later)
?                   ^
+ 12:04 AM, Friday (2 days later)
?                   ^
 : Expected calling "add_time()" with "2:59 AM", "24:00", "Friday" to return "12:04 AM, Friday (2 days later)"

----------------------------------------------------------------------
Ran 12 tests in 0.004s

FAILED (failures=3)
 

There’s actually an incorrect message in the test_two_days_later_with_day test (in the test, not your code, that I just noticed). I fixed that and reran them:

======================================================================
FAIL: test_high_duration_with_day (test_module.UnitTests)
----------------------------------------------------------------------
AssertionError: '6:18 AM, Monday (21 days later)' != '6:18 AM, Monday (20 days later)'
- 6:18 AM, Monday (21 days later)
?                   ^
+ 6:18 AM, Monday (20 days later)
?                   ^
 : Expected calling "add_time()" with "8:16 PM", "466:02", "tuesday" to return "6:18 AM, Monday (20 days later)"

======================================================================
FAIL: test_twenty_four_with_day (test_module.UnitTests)
----------------------------------------------------------------------
AssertionError: '2:59 AM, Sunday (6 days later)' != '2:59 AM, Sunday (next day)'
- 2:59 AM, Sunday (6 days later)
?                  ^    -------
+ 2:59 AM, Sunday (next day)
?                  ^^^^
 : Expected calling "add_time()" with "2:59 AM", "24:00", "saturDay" to return "2:59 AM, Sunday (next day)"

======================================================================
FAIL: test_two_days_later_with_day (test_module.UnitTests)
----------------------------------------------------------------------
AssertionError: '12:04 AM, Friday (4 days later)' != '12:04 AM, Friday (2 days later)'
- 12:04 AM, Friday (4 days later)
?                   ^
+ 12:04 AM, Friday (2 days later)
?                   ^
 : Expected calling "add_time()" with "11:59 AM", "24:05", "Friday" to return "12:04 AM, Friday (2 days later)"

----------------------------------------------------------------------

All three are day mismatches, so that’s where you need to look in the code to find the problem. The test_twenty_four_with_day indicates that your code has a problem with handling weeks and this may be caused partially by how you work through your week list; it looks like it wrapped around the list. The other two probably have an off-by-one day error and the last is probably also hitting a boundary issue in your AM/PM or midnight stuff. These are just guesses as I did not fully trace the code’s logic.

Near the top I think you have a good plan with this

pulling out the hours, and then the minutes; you may find it simplifies your logic if you do the same beginning with weeks and days as well.

Good luck.

thank you for the answer! hope i can fix my code with your tips.