Scientific Computing Python: Time calculator

Hello, I’ve recently started the Scientific Computing Module but I ran into a problem with the submission of the Time Calculator Project.

So I tested each call in the given test suite individually and the output seems correct. For example, I wrote the code: print(add_time('3:30 PM', '2:12'), which returns 5:42 PM.

However, when I run the tests, half of the tests failed. I tried looking at the chrome console but I still don’t understand why the test failed when the output is the same as what is expected.

More examples:
Calling add_time('8:16 PM', '466:02') passed.
Calling add_time('3:30 PM', '2:12') failed even though the output appears to be the same as the requirement.

Below is a the full function I have written:

def add_time(start, duration, day_name=None):
    # sanitizing input
    day_name = day_name.title() if day_name else None
    day_names = {
        0: ', Sunday',
        1: ', Monday', 
        2: ', Tuesday', 
        3: ', Wednesday', 
        4: ', Thursday', 
        5: ', Friday', 
        6: ', Saturday', 
        7: ', Sunday',
        'Monday': 1,
        'Tuesday': 2,
        'Wednesday': 3,
        'Thursday': 4,
        'Friday': 5,
        'Saturday': 6,
        'Sunday': 7
    }

    new_time = [] # all values should be strings
    start_time, abbriv = start.split()
    hour, minute = list(map(int, start_time.split(':'))) # initial time
    hour_1, minute_1 = list(map(int, duration.split(':'))) # time to add

    # convert all time to 24 hour format
    if abbriv == 'PM':
        hour = int(hour) + 12

    # quotient added onto hours
    # remainder is the new minutes section
    add_hour, new_minute = divmod(minute+minute_1, 60)
    new_time.insert(0, str(new_minute) if len(str(new_minute))==2 else '0'+str(new_minute))
    add_day, new_hour = divmod(hour+hour_1+add_hour, 24)
    new_time.insert(0, str(new_hour))


    # convert back to 12 hour format
    new_abbriv = 'AM'
    if int(new_time[0]) > 12:
        new_time[0] = int(new_time[0]) - 12
        new_abbriv = 'PM'
    # special case between 12PM and 1PM
    elif int(new_time[0]) == 12:
        new_abbriv = 'PM'
    # special case right after midnight
    elif int(new_time[0]) == 0:
        new_time[0] = int(new_time[0]) + 12
        new_abbriv = 'AM'

    message = ''
    if add_day > 0:
        if add_day == 1:
            message = '(next day)'
        else:
            message = f'({add_day} days later)'

    new_day_name = ''
    # dealing with day names
    if day_name:
        new_day_name = day_names[(day_names[day_name] + int(add_day))%7]

    return f"{':'.join(map(str, new_time))} {new_abbriv}{new_day_name} {message}"


print(add_time('3:30 PM', '2:12'))

I would appreciate any help at all.

There’s one detail causing it. Take a look at the printout of the following:

print('"' + add_time('3:30 PM', '2:12') + '"')

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.