Time calculator

Hi everybody

I have one failure with my code that I could not fix, if anybody can review the code and help me with this.
The failure is related to the specific case of showing the day (“monday”) within the same day.

> FAIL: test_same_period_with_day (test_module.UnitTests)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File "/home/runner/timecalculator/test_module.py", line 50, in test_same_period_with_day
>     self.assertEqual(actual, expected, 'Expected calling "add_time()" with "3:30 PM", "2:12", "Monday" to return "5:42 PM, Monday"')
> AssertionError: '5:42 PM' != '5:42 PM, Monday'
> - 5:42 PM
> + 5:42 PM, Monday
>  : Expected calling "add_time()" with "3:30 PM", "2:12", "Monday" to return "5:42 PM, Monday"

Here is the code for review, thanks

import math

def add_time(cur_time, time_add, day_of_week=" "):
    current_time, period = cur_time.split(' ')
    current_hour, current_minute = current_time.split(':')
    hour_to_add, minutes_to_add = time_add.split(':')
    day_of_week = day_of_week.lower()
    days_of_the_week = ["saturday", "sunday", 'Monday', "tuesday", "wednesday",
                        "thursday", "friday"]

    if period == "AM":
        periods = 0
    elif period == "PM":
        periods = 1

    # converting the hours into minutes and casting the minutes
    total_minutes_std_hour = (int(current_hour.strip()) * 60)
    total_minutes_add_hours = (int(hour_to_add.strip()) * 60)
    std_minutes = int(current_minute.strip())
    add_minutes = int(minutes_to_add.strip())

    # total of hours combined
    total_minutes = int(total_minutes_std_hour + total_minutes_add_hours + std_minutes + add_minutes)
    total_minutes_left = int ((total_minutes_std_hour + total_minutes_add_hours + std_minutes + add_minutes) % 60)
    total_hours = total_minutes / 60

    periods += math.ceil(total_hours / 12)
    period_indicator = ' '
    if periods % 2 != 0:
        period_indicator = "AM"
    else:
        period_indicator = "PM"

    # data that will be printed in the clock
    clock_minutes = total_minutes_left

    if clock_minutes < 10:
        clock_minutes = '0' + str(clock_minutes)

    clock_hour = total_hours
    days = periods / 2
    day_message = 0

    if clock_hour >= 12:
        clock_hour = clock_hour % 12

        if (period_indicator == "PM" or period_indicator == "AM") and int(clock_hour) == 0:
            clock_hour = 12

    if float(days) <= 1:
        day_message = ""

    elif float(days) > 1 and days <= 2:
        day_message = "(next day)"

    elif days > 2:
        day_message = f"({str(int(days))} days later)"

    if day_of_week not in days_of_the_week:
        if day_message == "":
            returning_data = f"{str(int(clock_hour))}:{str(clock_minutes)} {period_indicator}"
        else:
            returning_data = f"{str(int(clock_hour))}:{str(clock_minutes)} {period_indicator} {day_message}"

    else:

        days = math.ceil(days)
        if day_of_week == "monday":
            day = (1 + days) % 7
            day_of_week = days_of_the_week[day].capitalize()

        elif day_of_week == "tuesday":
            day = (2 + days) % 7
            day_of_week = days_of_the_week[day].capitalize()

        elif day_of_week == "wednesday":
            day = (3 + days) % 7
            day_of_week = days_of_the_week[day].capitalize()

        elif day_of_week == "thursday":
            day = (4 + days) % 7
            day_of_week = days_of_the_week[day].capitalize()

        elif day_of_week == "friday":
            day = (5 + days) % 7
            day_of_week = days_of_the_week[day].capitalize()


        elif day_of_week == "saturday":
            day = (6 + days) % 7
            day_of_week = days_of_the_week[day].capitalize()

        else:
            day = days % 7
            day_of_week = days_of_the_week[day].capitalize()

        returning_data = f"{str(int(clock_hour))}:{str(clock_minutes)} {period_indicator}, {day_of_week} {day_message}"

    return returning_data


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36.

Challenge: Time Calculator

Link to the challenge:

The problem might be in the fact that you capitalized “Monday” in the days_of_the_week list.

@petrs thank you very much, that was indeed the error. I think I would never find it without an external eyes.
Cheers

Glad to help.

I just retraced the logic of the code in my head from the bottom (where the problem surfaced) up until I found the discrepancy.

You can also consider getting better at debugging (https://stribny.name/blog/2019/06/debugging-python-programs/) to be able to examine the problem more quickly.

Have a nice day!

Thanks for the resource.