Build a Time Calculator Project - Build a Time Calculator Project

Tell us what’s happening:

I am facing a weird problem in this time calculator project, the code passes the test cases when i run it (in vsc) but it doesn’t pass some of the test cases when i submit even though it is returning the exact same thing it is to return

Your code so far

days = {
    "": "",
    "Sunday": 1,
    "Monday": 2,
    "Tuesday": 3,
    "Wednesday": 4,
    "Thursday": 5,
    "Friday": 6,
    "Saturday": 7
}

def add_time(start, duration, day=""):
    #splitting hours, minutes, and am/pm
    arr = start.split()
    timeArr = arr[0].split(':')
    amPm = arr[1]
    hour = int(timeArr[0])
    minute = int(timeArr[1])
    
    addArr = duration.split(':')
    addHour = int(addArr[0])
    addMinute = int(addArr[1])
    
    #calculating new minute
    newMinute = minute + addMinute
    if newMinute >= 60:
        newMinute -= 60
        addHour += 1

    #calculating total hours and days
    totalHours = hour + addHour
    dayCount = totalHours // 24
    remainingHours = totalHours % 24
    
    #calculating new hour
    newHour = remainingHours % 12
    if newHour == 0:
        newHour = 12
    
    #determining am/pm
    periodCount = (hour + addHour) // 12
    isPm = amPm == "PM"
    newAmPm = "PM" if (periodCount + isPm) % 2 else "AM"
    
    #am/pm change at 12
    if remainingHours >= 12:
        dayCount += 1 if isPm else 0
    else:
        dayCount += 1 if isPm and (hour + addHour) >= 12 else 0
    
    #day calc
    if day:
        dayIndex = (days[day.capitalize()] + dayCount - 1) % 7 + 1
        newDay = None
        for k, v in days.items():
            if v == dayIndex:
                newDay = k
                break
    else:
        newDay = ""
    
    #days later message
    if dayCount == 0:
        daysLaterMessage = ""
    elif dayCount == 1:
        daysLaterMessage = "(next day)"
    else:
        daysLaterMessage = f"({dayCount} days later)"
    
    if newDay:
        new_time = f"{newHour}:{newMinute:02d} {newAmPm}, {newDay} {daysLaterMessage}"
    else:
        new_time = f"{newHour}:{newMinute:02d} {newAmPm} {daysLaterMessage}"

    return new_time


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Build a Time Calculator Project - Build a Time Calculator Project

Hi there and welcome to our community!

Your issue is in your return strings. Specifically, in some cases your strings have a stray space on the end of them (though this is an ‘invisible’ error).
For instance, you are tacking on an empty daysLaterMessage string in some cases, which includes a space before it.
You could fix this quite easily by only concatenating extant values onto your return string (i.e. don’t include daysLaterMessage if it’s an empty string).

If you add a print call like this (using the repr() function) you’ll see where you have surplus spaces, as it shows all the ‘hidden’ spaces:

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

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