Build a Time Calculator Project - Build a Time Calculator Project

Tell us what’s happening:

From what I see, the output is correct for all tests so I’m not sure exactly what’s wrong. I’ve been missing around with errors for AM-PM conversion but I think it should work for all valid inputs like this, I’ve had an error with the optional entry and leaving a space but I think I also took care of that. Any tips on more efficient code would also be greatly appreciated !

Your code so far



def add_time(start, duration, day=''):
    #all whole integers 
    Pairings = [(1, 13), (2, 14), (3, 15), (4, 16), (5, 17), (6, 18), (7, 19), (8, 20), (9, 21), (10, 22), (11, 23), (12, 24)]
    #all week days 
    Week_days = ['saturday', 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday']
    #split input into desigination and time parts 
    start_parts = start.split() #time and desg 
    desg = start_parts[1]
    t_split = start_parts[0].split(':')
    t_h = int(t_split[0])
    t_min = int(t_split[1])
    d_parts = duration.split(':') #time only 
    t_h_in = int(d_parts[0])
    t_min_in = int(d_parts[1])
#convert input pm values to true values and keep am values 
    for AM, PM in Pairings:
        if desg == 'PM' and t_h != 12 and t_h == AM:
            t_h = PM
            break
        elif desg == 'AM' and t_h == AM:
            t_h = AM
            break
    H = t_h_in + t_h
    t_days = 0 
#deal with minutes 
    if t_min_in >= 60:
        return 'Invalid input'
    t_min = t_min + t_min_in
    if t_min >= 60:
        H += t_min // 60 #get hour overflow  
        t_min = t_min % 60 #get minute overflow regardless of hours 
        if t_min < 10:  
            t_min = '0' + str(t_min) #fix format if 1 decimal integer for overflow  
    elif t_min < 10: #if no overflow and 1 decimal integer 
        t_min = '0' + str(t_min)  
#at this point we have H as the total hours
    if 48 > H > 24: #if 1 day passes with some hours 
        t_days += 1 #increase day counter 
        H = H % 24 #overflow hours 
    if H >= 48: #if 2 or more than 2 days pass 
        t_days = H // 24 #overflow days 
        H = H % 24 #over flow hours is hour on 1-24 scale 
#reconvert back to AM, PM if PM 
    if H <= 24: 
        pass #no change in Hours value, same scale 
#we define any hour from 1-24 as 1-12 and 1-12 where 12 pm = 12 and 12 am = 24 (=0 for mod)
    for AM, PM in Pairings:
        if H == PM: #if final value is in PM (PM defined as 13-24, H only possible for 1-23)
            H = AM #convert final value to counter part 
            desg = 'PM' #assure PM desigination  
            break
        elif H == 12: #if 12 switch to PM 
            desg = 'PM' #ensure 12 PM 
            break
        elif H == 0:  #if 24mod24 = 0 
            H = 12  
            desg = 'AM' #ensure switch to 12 AM 
            break
        elif H < 12: 
            desg = 'AM' #else keep am value as is (12 > must be in AM scale)
            break
    results = False  
#add optional day input 
    if day != '':
        results = True 
        day = day.lower()
        for i, days in enumerate(Week_days):
           if day == days in Week_days:
               day = i
               day_index = (day + t_days) % 7 #total days in cycle % 7  
               break 
    elif day == '':
        results = False
#set output based on previous if elif conditions 
    if t_days == 0 and results:
        output = f'{H}:{t_min} {desg}, {Week_days[day_index].capitalize()}'
    elif t_days == 0 and not results:
        output = f'{H}:{t_min} {desg}'
    elif t_days == 1 and results:
        output = f'{H}:{t_min} {desg}, {Week_days[day_index].capitalize()} (next day)'
    elif t_days == 1 and not results:
        output = f'{H}:{t_min} {desg} (next day)'
    elif t_days > 1 and results:
        output = f'{H}:{t_min} {desg}, {Week_days[day_index].capitalize()} ({t_days} days later)'
    elif t_days > 1 and not results:
        output = f'{H}:{t_min} {desg} ({t_days} days later)'
    return print(output)


add_time('3:30 PM', '2:12')
add_time('11:55 AM', '3:12')
add_time('2:59 AM', '24:00')
add_time('11:59 PM', '24:05')
add_time('8:16 PM', '466:02')
add_time('3:30 PM', '2:12', 'Monday')
add_time('2:59 AM', '24:00', 'saturDay')
add_time('11:59 PM', '24:05', 'Wednesday')
add_time('8:16 PM', '466:02', 'tuesday')

                

### Your browser information:

User Agent is: <code>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 OPR/115.0.0.0 (Edition std-2)</code>

### Challenge Information:
Build a Time Calculator Project - Build a Time Calculator Project
https://www.freecodecamp.org/learn/scientific-computing-with-python/build-a-time-calculator-project/build-a-time-calculator-project

If you wrap the example function call with print, you will see what function actually returns.

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

What should be returned is the string with calculated time.

1 Like

I see, so the issue was that I returned the print and not output, and the intent is to use print for the function call.

Thank you !

1 Like