Build a Time Calculator Project - Build a Time Calculator Project

Tell us what’s happening:

Hello. I’m back at certification stuff, and I’m trying to get python now. The time calculator’s logic is mostly done, but there’s a slight issue when counting how many days have passed, and will sometimes return one less than it should

Your code so far

print('\nTIME CALCULATOR\n')

def add_time(start, duration, day_of_week = '', days = 0, init_am_pm = False, recursion = 0):
    #The extra parameters are just for recursion purposes
    recursion_num = recursion

    if not init_am_pm:
        print(f'\n==========================\n\nSTART OF TIME OPERATION\n\n--------------------------\n\nMain calculation\n')
    else:
        recursion_num += 1
        print(f'--------------------------\n\nRecursive calculation #{recursion_num}\n')
    start_first_num = 0
    start_last_num = 0
    am_pm = '-\_(ツ)_/-'

    days_of_the_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

    day_of_the_week = ''

    if len(start) == 8:
        start_first_num = int(start[0] + start[1])
        start_last_num = int(start[3] + start[4])
        am_pm = start[6] + start[7]
    else:
        start_first_num = int(start[0])
        start_last_num = int(start[2] + start[3])
        am_pm = start[5] + start[6]

    d_first_num = 0
    d_last_num = 0

    if len(duration) == 6:
        d_first_num = int(duration[0] + duration[1] + duration[2])
        d_last_num = int(duration[4] + duration[5])
    elif len(duration) == 5:
        d_first_num = int(duration[0] + duration[1])
        d_last_num = int(duration[3] + duration[4])
    else:
        d_first_num = int(duration[0])
        d_last_num = int(duration[2] + duration[3])

    passed_days = days

    print(f'Passed days: {passed_days}\n')

    print(start_first_num, ':', start_last_num, am_pm, '\n')
    print(d_first_num, ':' , d_last_num, '\n')

    new_first_num = start_first_num + d_first_num
    new_last_num = start_last_num + d_last_num

    print(new_first_num, ':', new_last_num, '\n')

    overflow = 0

    while new_last_num >= 60:
        new_last_num -= 60
        overflow += 1

    while new_first_num > 12:
            new_first_num -= 1
            overflow += 1

    if am_pm == init_am_pm:
        passed_days += 1

    new_time = str(new_first_num) + ':' + str(new_last_num) + ' ' + am_pm
    print(new_time, f', Plus {overflow} hours\n')
    
    if overflow:
        if new_first_num + overflow <= 12:
            new_first_num += overflow
            overflow = 0
        else:
            if am_pm == 'AM':
                am_pm = 'PM'
            else:
                am_pm = 'AM'

            overflow -= 12
            if overflow < 0:
                new_first_num += overflow

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

    print(am_pm, init_am_pm, '\n')

    initial_period = init_am_pm
    
    if not init_am_pm:
        initial_period = am_pm

    if day_of_week:
        day_of_the_week = day_of_week.title()
        day_of_the_week = days_of_the_week[(days_of_the_week.index(day_of_the_week) + passed_days) % 7]

    if day_of_the_week:
        am_pm += ', ' + day_of_the_week

    if passed_days == 1:
        am_pm += ' (next day)'
    elif passed_days > 1:
        am_pm += f' ({passed_days} days later)'

    new_time = str(new_first_num) + ':' + str(new_last_num) + ' ' + am_pm
    print(new_time, f', Plus {overflow} hours\n')

    if overflow > 0:
        # D I V I N E   R E C U R S I O N ! ! ! ! 
        return add_time(str(new_first_num) + ':' + str(new_last_num) + ' ' + am_pm[0] + am_pm[1], f'{overflow}:00', day_of_the_week, passed_days, initial_period, recursion_num)
    else:
        return new_time

print(f"--------------------------\n\nEnd of time operation. Result is: [{add_time('11:59 PM', '24:05', 'Wednesday')}]\n")

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36

Challenge Information:

Build a Time Calculator Project - Build a Time Calculator Project

it seems you have a logic issue when it is 11am or 11 pm
because the time should go as follow:
11:58 AM
11:59 AM
12:00 PM
12:01 PM

If you call add_time('11:59 AM', '00:01') the result is 12:00 AM.

Same for
11:58 PM
11:59 PM
12:00 AM
12:01 AM

If you call add_time('11:59 PM', '00:01') the result is 12:00 PM.

Can you fix it?

Stuff was just fixed!

print('\nTIME CALCULATOR\n')

def add_time(start, duration, day_of_week = '', days = 0, init_am_pm = False, recursion = 0):

    recursion_num = recursion

    if not init_am_pm:
        print(f'\n==========================\n\nSTART OF TIME OPERATION\n\nInput: [add_time({start}, {duration}, {day_of_week})]\n\n--------------------------\n\nMain calculation\n')
    else:
        recursion_num += 1
        print(f'--------------------------\n\nRecursive calculation #{recursion_num}\n\nFunction call: [add_time({start}, {duration}, {day_of_week}, {days}, {init_am_pm}, {recursion})]\n')
    start_first_num = 0
    start_last_num = 0
    am_pm = '-\_(ツ)_/-'

    days_of_the_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

    day_of_the_week = ''

    if len(start) == 8:
        start_first_num = int(start[0] + start[1])
        start_last_num = int(start[3] + start[4])
        am_pm = start[6] + start[7]
    else:
        start_first_num = int(start[0])
        start_last_num = int(start[2] + start[3])
        am_pm = start[5] + start[6]

    d_first_num = 0
    d_last_num = 0

    if len(duration) == 6:
        d_first_num = int(duration[0] + duration[1] + duration[2])
        d_last_num = int(duration[4] + duration[5])
    elif len(duration) == 5:
        d_first_num = int(duration[0] + duration[1])
        d_last_num = int(duration[3] + duration[4])
    else:
        d_first_num = int(duration[0])
        d_last_num = int(duration[2] + duration[3])

    passed_days = days

    print(start_first_num, ':', start_last_num, am_pm, '\n')
    print(d_first_num, ':' , d_last_num, '\n')

    new_first_num = start_first_num + d_first_num
    new_last_num = start_last_num + d_last_num

    print(new_first_num, ':', new_last_num, '\n')

    overflow = 0

    while new_last_num >= 60:
        new_last_num -= 60
        overflow += 1

    while new_first_num > 12:
            new_first_num -= 1
            overflow += 1

    if am_pm == init_am_pm:
        passed_days += 1

    new_time = str(new_first_num) + ':' + str(new_last_num) + ' ' + am_pm
    print(new_time, f', Plus {overflow} hours\n')
    
    if overflow:
        if new_first_num + overflow <= 12:
            new_first_num += overflow
            overflow = 0
        else:
            if am_pm == 'AM':
                am_pm = 'PM'
            else:
                am_pm = 'AM'

            overflow -= 12
            if overflow < 0:
                new_first_num += overflow

    #Thanks to ILM for helping with this bit!
    #https://forum.freecodecamp.org/t/build-a-time-calculator-project-build-a-time-calculator-project/723412

    if start_first_num < 12 and new_first_num >= 12:
        if am_pm == 'AM':
                am_pm = 'PM'
        else:
            am_pm = 'AM'

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

    print(am_pm, init_am_pm, '\n')

    initial_period = init_am_pm
    
    if not init_am_pm:
        initial_period = am_pm

    if am_pm == initial_period:
        passed_days += 1

    if day_of_week:
        day_of_the_week = day_of_week.title()
        day_of_the_week = days_of_the_week[(days_of_the_week.index(day_of_the_week) + passed_days) % 7]

    if day_of_the_week:
        am_pm += ', ' + day_of_the_week

    print(f'Passed days: {passed_days}\n')

    if passed_days == 1:
        am_pm += ' (next day)'
    elif passed_days > 1:
        am_pm += f' ({passed_days} days later)'

    new_time = str(new_first_num) + ':' + str(new_last_num) + ' ' + am_pm
    print(new_time, f', Plus {overflow} hours\n')

    if overflow > 0:
        # D I V I N E   R E C U R S I O N ! ! ! ! 
        return add_time(str(new_first_num) + ':' + str(new_last_num) + ' ' + am_pm[0] + am_pm[1], f'{overflow}:00', day_of_the_week, passed_days, initial_period, recursion_num)
    else:
        return new_time

print(f"--------------------------\n\nEnd of time operation. Result is: [{add_time('11:59 PM', '24:05', 'Wednesday')}]\n")

Only issue now is it gets days of the week wrong for some reason

You have more issues than just the days of the week:
add_time('3:30 PM', '2:12') has a result with next day, it should still be the same day

are you checking the output of the function calls from the failing tests?

yeah, just noticed.

I am looking at them, but not from the browser console. Just adding more and more add_time calls at the bottom

either way works, as long as you try to do some debugging

It is finally almost functional, but, for some reason, all tests with an output that is supposed to end with 2 days later (namely tests 6 and 11) are still failing, and the result instead ends with next day

print('\nTIME CALCULATOR\n')

def add_time(start, duration, day_of_week = '', days = 0, init_am_pm = False, recursion = 0):
    #The extra parameters are just for recursion purposes
    recursion_num = recursion

    if not init_am_pm:
        print(f'\n==========================\n\nSTART OF TIME OPERATION\n\nInput: [add_time({start}, {duration}, {day_of_week})]\n\n--------------------------\n\nMain calculation\n')
    else:
        recursion_num += 1
        print(f'--------------------------\n\nRecursive calculation #{recursion_num}\n\nFunction call: [add_time({start}, {duration}, {day_of_week}, {days}, {init_am_pm}, {recursion})]\n')
    start_first_num = 0
    start_last_num = 0
    am_pm = '-\_(ツ)_/-'

    days_of_the_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

    day_of_the_week = ''

    if len(start) == 8:
        start_first_num = int(start[0] + start[1])
        start_last_num = int(start[3] + start[4])
        am_pm = start[6] + start[7]
    else:
        start_first_num = int(start[0])
        start_last_num = int(start[2] + start[3])
        am_pm = start[5] + start[6]

    d_first_num = 0
    d_last_num = 0

    if len(duration) == 6:
        d_first_num = int(duration[0] + duration[1] + duration[2])
        d_last_num = int(duration[4] + duration[5])
    elif len(duration) == 5:
        d_first_num = int(duration[0] + duration[1])
        d_last_num = int(duration[3] + duration[4])
    else:
        d_first_num = int(duration[0])
        d_last_num = int(duration[2] + duration[3])

    passed_days = days

    print(start_first_num, ':', start_last_num, am_pm, '\n')
    print(d_first_num, ':' , d_last_num, '\n')

    new_first_num = start_first_num + d_first_num
    new_last_num = start_last_num + d_last_num

    print(new_first_num, ':', new_last_num, '\n')

    overflow = 0

    while new_last_num >= 60:
        new_last_num -= 60
        overflow += 1

    while new_first_num > 12:
            new_first_num -= 1
            overflow += 1

    new_time = str(new_first_num) + ':' + str(new_last_num) + ' ' + am_pm
    print(new_time, f', Plus {overflow} hours\n')
    
    if overflow:
        if new_first_num + overflow <= 12:
            new_first_num += overflow
            overflow = 0

            #Thanks to ILM for helping with this bit!
            #https://forum.freecodecamp.org/t/build-a-time-calculator-project-build-a-time-calculator-project/723412

            if start_first_num < 12 and new_first_num >= 12:
                if am_pm == 'AM':
                        am_pm = 'PM'
                else:
                    am_pm = 'AM'
                    passed_days += 1
        else:
            if am_pm == 'AM':
                am_pm = 'PM'
            else:
                am_pm = 'AM'
                passed_days += 1

            overflow -= 12
            if overflow < 0:
                new_first_num += overflow

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

    print(am_pm, init_am_pm, '\n')
    print(f'Passed days: {passed_days}\n')

    initial_period = init_am_pm
    
    if not init_am_pm:
        initial_period = am_pm

    if day_of_week:
        day_of_the_week = day_of_week.title()
        day_of_the_week = days_of_the_week[(days_of_the_week.index(day_of_the_week) + passed_days) % 7]

    if day_of_the_week:
        am_pm += ', ' + day_of_the_week

    if passed_days == 1:
        am_pm += ' (next day)'
    elif passed_days > 1:
        am_pm += f' ({passed_days} days later)'

    new_time = str(new_first_num) + ':' + str(new_last_num) + ' ' + am_pm
    print(new_time, f', Plus {overflow} hours\n')

    if overflow > 0:
        # D I V I N E   R E C U R S I O N ! ! ! ! 
        return add_time(str(new_first_num) + ':' + str(new_last_num) + ' ' + am_pm[0] + am_pm[1], f'{overflow}:00', day_of_week, passed_days, initial_period, recursion_num)
    else:
        return new_time

print(f"--------------------------\n\nEnd of time operation. Result is: [{add_time('11:59 PM', '24:05', 'Wednesday')}]\n")