Build a Time Calculator Project - Build a Time Calculator Project

Tell us what’s happening:

I got stuck with tests No. 3 and No. 8.
No clues whatsoever how to deal with these because they are not related to any specific time calculation example. I followed Socrates’ advice and did some refactoring, also I’m paying close attention to the output in the console. But I just don’ t get to pass No. 3 and No. 8.

Your code so far


Your browser information:

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

Challenge Information:

Build a Time Calculator Project - Build a Time Calculator Project

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/build-a-time-calculator-project/5e444136903586ffb414c94d.md at main · freeCodeCamp/freeCodeCamp · GitHub

Your code was too long to be automatically inserted by the help button.

Please update the message, or reply to this one, to include your code, formatted as follows:

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

def day_offsetter(lst, offset, number):
    for i in range(number):
        yield lst[(offset + i) % len(lst)]



def add_time(start, duration, start_day=False):
    day_counter = 2
    limit = 11.59
    sixty = 60
    day = 24.00
    w_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    while 'AM' in start:
        start_hrs_am = start.strip(' AM').replace(':', '.')
        duration_am = duration.replace(':', '.')
        time_til_noon = round((limit - float(start_hrs_am)), 2) + 00.01
        # 1st case: end time will stay at 'AM'
        if time_til_noon > float(duration_am):
            end_hrs_am_temp = round(float(start_hrs_am) + float(duration_am), 2)
            end_hrs_am_temp = str(end_hrs_am_temp)
            if end_hrs_am_temp[1] == '.':
                hour, minute = divmod(int(end_hrs_am_temp[2:]), 60)
                ending_hr_am = int(end_hrs_am_temp[0]) + hour
                end_hrs_am = end_hrs_am_temp.replace(end_hrs_am_temp[0], str(ending_hr_am))
            else:
                hour, minute = divmod(int(end_hrs_am_temp[3:]), 60)
                ending_hr_am = int(end_hrs_am_temp[:2]) + hour
                end_hrs_am = end_hrs_am_temp.replace(end_hrs_am_temp[:2], str(ending_hr_am))
            if end_hrs_am[1] == '.':
                end_hrs_am = end_hrs_am.replace(end_hrs_am[2:], str(minute))
                end_hrs_am = end_hrs_am.replace('.', ':')
            else:
                end_hrs_am = end_hrs_am.replace(end_hrs_am[3:], str(minute))
                end_hrs_am = end_hrs_am.replace('.', ':')
            while start_day:
                start_day = start_day.casefold().capitalize()
                new_time = f'{end_hrs_am} AM, {start_day}'
                return new_time
            new_time = f'{end_hrs_am} AM'
            return new_time
        # 2nd case: 24 hours later
        elif float(duration_am) == day and float(duration_am) >= time_til_noon:


            while start_day:
                pos_start_day = [day.casefold() for day in w_days].index(start_day.casefold()) # for case insensitivity
                end_day = list(day_offsetter(w_days, pos_start_day, day_counter))[-1]
                start_hrs_am = start_hrs_am.replace('.', ':')
                new_time = f'{start_hrs_am} AM, {end_day} (next day)'
                return new_time
            start_hrs_am = start_hrs_am.replace('.', ':')
            new_time = f'{start_hrs_am} AM (next day)'
            return new_time
        
        
        # 3rd case: change from 'AM' to 'PM'
        elif float(duration_am) >= time_til_noon:

            time_after_noon = round((float(duration_am) - float(time_til_noon)), 2)
            end_hrs_am = 00.00 + float(time_after_noon)
            end_hrs_am = str(end_hrs_am)
            end_hrs_am = end_hrs_am.replace('.', ':')
            if end_hrs_am.startswith('00'):
                end_hrs_am = end_hrs_am.replace('00', '12', 1)
            elif end_hrs_am.startswith('0'):
                end_hrs_am = end_hrs_am.replace('0', '12', 1)
            while start_day:
                start_day = start_day.casefold().capitalize()
                new_time = f'{end_hrs_am} PM, {start_day}'
                return new_time
            new_time = f'{end_hrs_am} PM'
            return new_time
    # 'PM' in 'start'
    start_hrs_pm = start.strip(' PM')
    start_hrs_pm = start_hrs_pm.replace(':', '.')
    duration_pm = duration.replace(':', '.')
    end_hrs_pm = round(float(start_hrs_pm) + float(duration_pm), 2)
    end_hrs_pm = str(end_hrs_pm)
    endzero = end_hrs_pm.rfind('.')+1
    if len(end_hrs_pm[endzero:]) == 1:
        end_hrs_pm = list(end_hrs_pm)
        end_hrs_pm.append('0')
        end_hrs_pm = ''.join(end_hrs_pm)
    # 1st case: end time will stay at 'PM' (same day)
    while float(end_hrs_pm) <= limit:
        end_hrs_pm = str(end_hrs_pm)
        end_hrs_pm = end_hrs_pm.replace('.', ':')
        while start_day:
            pos_start_day = [day.casefold() for day in w_days].index(start_day.casefold()) # for case insensitivity
            
            end_day = start_day
            new_time = f'{end_hrs_pm} PM, {end_day}'
            return new_time
        new_time = f'{end_hrs_pm} PM'
        return new_time
    # 2nd case: change of day (from 'PM' to 'AM')
    time_til_midnight = round((limit - float(start_hrs_pm)), 2) + 00.01
    time_til_midnight = str(time_til_midnight)
    endzeero = time_til_midnight.rfind('.')+1
    if len(time_til_midnight[endzeero:]) == 1:
        time_til_midnight = list(time_til_midnight)
        time_til_midnight.append('0')
        time_til_midnight = ''.join(time_til_midnight)
    # Basic condition for change of day
    while float(duration_pm) > float(time_til_midnight):
        if time_til_midnight[-2:] > duration_pm[-2:]:
            part_of_hour1 = int(time_til_midnight[-2:]) / sixty
            part_of_hour2 = int(duration_pm[-2:]) / sixty
            part_of_hour3 = part_of_hour1 - part_of_hour2
            part_of_hour4 = part_of_hour3 * sixty
            part_of_hour5 = round(sixty - part_of_hour4)
            time_after_midnight = round(float(duration_pm) - float(time_til_midnight), 2)
            time_after_midnight = str(time_after_midnight)
            endzeeero = time_after_midnight.rfind('.')+1
            if len(time_after_midnight[endzeeero:]) == 1:
                time_after_midnight = list(time_after_midnight)
                time_after_midnight.append('0')
                time_after_midnight = ''.join(time_after_midnight)
            time_after_midnight = time_after_midnight.replace(time_after_midnight[-2:], str(part_of_hour5))
            end_hrs_pm = 00.00 + float(time_after_midnight)
            end_hrs_pm = str(end_hrs_pm)
            endzeeeero = end_hrs_pm.rfind('.')+1
            if len(end_hrs_pm[endzeeeero:]) == 1:
                end_hrs_pm = list(end_hrs_pm)
                end_hrs_pm.append('0')
                end_hrs_pm = ''.join(end_hrs_pm)
            if float(end_hrs_pm) < day:
                end_hrs_pm = str(end_hrs_pm)
                end_hrs_pm = end_hrs_pm.replace('.', ':')
                new_time = f'{end_hrs_pm} AM (next day)'
                return new_time
            else:
                # Case: day after next and beyond
        
                end_hrs_pm = float(end_hrs_pm)
                day_divider = end_hrs_pm - day
                while True:
                    day_divider -= day
                    day_counter += 1
                    if day_divider < day:
                        break
                end_hrs_pm = round(day_divider, 2)     
                end_hrs_pm = str(end_hrs_pm)
                if len(end_hrs_pm) < 5 and not end_hrs_pm.startswith('0'):
                    end_hrs_pm = list(end_hrs_pm)
                    end_hrs_pm.insert(0, '0')
                    end_hrs_pm = ''.join(end_hrs_pm)
                if end_hrs_pm.startswith('0'):
                    end_hrs_pm = end_hrs_pm.replace('0', '', 1)
                end_hrs_pm = str(end_hrs_pm)  
                end_hrs_pm = end_hrs_pm.replace('.', ':')
                while start_day:
                    pos_start_day = [day.casefold() for day in w_days].index(start_day.casefold()) # for case insensitivity
                    end_day = list(day_offsetter(w_days, pos_start_day, day_counter + 1))[-1]
                    new_time = f'{end_hrs_pm} AM, {end_day} ({day_counter} days later)'
                    return new_time
                new_time = f'{end_hrs_pm} AM ({day_counter} days later)'
                return new_time
        else:
            # Change from one day to the next ('PM' to 'AM')
            time_after_midnight = round(float(duration_pm) - float(time_til_midnight), 2)
            end_hrs_pm = 00.00 + float(time_after_midnight)
            end_hrs_pm = str(end_hrs_pm)
            # Case: day after next and beyond
            day_divider = round(float(end_hrs_pm) - day, 2)
            day_divider = str(day_divider)
            if len(day_divider) < 5 or not day_divider.startswith('0'):
                day_divider = list(day_divider)
                day_divider.insert(0, '0')
                day_divider = ''.join(day_divider)
            day_divider = float(day_divider)
            while day_divider > day:
                day_divider -= day
                day_counter += 1
            end_hrs_pm = round(day_divider, 2)
            end_hrs_pm = str(end_hrs_pm)
            end_hrs_pm = end_hrs_pm.replace('.', ':')
            # For first hour past midnight
            if end_hrs_pm.startswith('00'):
                end_hrs_pm = end_hrs_pm.replace('00', '12', 1)
            elif end_hrs_pm.startswith('0'):
                end_hrs_pm = end_hrs_pm.replace('0', '12', 1)
            while start_day:
                pos_start_day = [day.casefold() for day in w_days].index(start_day.casefold()) # for case insensitivity
                end_day = pos_start_day + 2
                end_day_plus_two = w_days[end_day]
                new_time = f'{end_hrs_pm} AM, {end_day_plus_two} ({day_counter} days later)'
                return new_time
            new_time = f'{end_hrs_pm} AM ({day_counter} days later)'
        return new_time


print(add_time('10:10 PM', '3:30'))

Hi @dizzysearchr,

Try calling your function like this to help you debug:

print(add_time('5:01 AM', '0:00'))

And what does this function call show in the console?

print(add_time('9:15 PM', '5:30'))

Happy coding

Yeah, I can see it. Thanks.