Build a Time Calculator Project - Build a Time Calculator Project

Tell us what’s happening:

All my returned output seems to match the answer but they didn’t all pass. Help?

Your code so far

def add_time(start, duration, day=""):
    start_m = start[-2::]
    start_hr = int(start.split(":")[0])
    # Turn start hour into 24hr
    if start_hr == 12 and start_m == "AM":
        start_hr = 0
    elif start_hr == 12 and start_m == "PM":
        start_hr = 12
    elif start_m == "PM":
        start_hr += 12
    start_min = int(start[-5:-3])
    duration_hr = int(duration.split(":")[0])
    duration_min = int(duration.split(":")[1])
    week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
    extra_day = 0
    new_day = ""
    new_m = ""

    # Calculate new minute
    new_min = start_min + duration_min
    if new_min >= 60:
        duration_hr = duration_hr + new_min // 60
        new_min %= 60
    if new_min == 0:
        new_min = "00"
    elif new_min > 0 and new_min < 10:
        new_min = f"0{new_min}"

    # Calculate new hour
    new_hr = start_hr + duration_hr
    if new_hr >= 24:
        extra_day = new_hr // 24
        new_hr %= 24
    if new_hr < 12:
        new_m = "AM"
    elif new_hr >= 12 and new_hr < 24:
        new_m = "PM"
        new_hr -= 12
    if new_hr == 0:
        new_hr = 12

    if day != "":
        day = day.lower()
        day_index = week.index(day)
        new_day_of_week = week[(day_index + extra_day) % 7].capitalize()
    # Calculate new hr in new day
    if extra_day == 1:
        new_day = "(next day)"
    elif extra_day > 1:
        new_day = f"({extra_day} days later)"
    
    if day == "":
        new_time = f"{new_hr}:{new_min} {new_m} {new_day}"
    else:
        new_time = f"{new_hr}:{new_min} {new_m}, {new_day_of_week} {new_day}"

    print(new_time)
    return new_time

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15

Challenge Information:

Build a Time Calculator Project - Build a Time Calculator Project

Hi and welcome to the forum :wave:

If you use repr() to see raw string output:

print(repr(add_time('3:30 PM', '2:12')))
>>> '5:42 PM '

Calling add_time('3:30 PM', '2:12') should return '5:42 PM'

wow… what a silly mistake. Thank you for the welcome and helping me catching it. I was tearing my hair out.

1 Like

Pretty common and hard to catch problem, I’d say. You cannot really tell just by looking at the print output alone. Good opportunity to learn another tool in the debugging toolbox!