Scientific Computing with Python Projects - Time Calculator

Tell us what’s happening:

Hi, I’m working on the Time Calculator project and still fail one test. I was working on it in replit, the Traceback there says I have one space appearing that shouldn’t be there… but I’m totally missing something, because I can’t find where it’s coming from.
Replit Traceback:

Traceback (most recent call last):
File “/home/runner/boilerplate-time-calculator/test_module.py”, line 50, in test_same_period_with_day
self.assertEqual(actual, expected, ‘Expected calling “add_time()” with “3:30 PM”, “2:12”, “Monday” to return “5:42 PM, Monday”’)
AssertionError: '5:42 PM, Monday ’ != ‘5:42 PM, Monday’

Your code so far

def add_time(start, duration, day=None):
    a = start.split()
    b = a[0].split(":")
    starting_hour = b[0]
    starting_minute = b[1]
    time_of_day = a[1]
    z = duration.split(":")
    duration_hour = z[0]
    duration_minute = z[1]

    total_hours = int(starting_hour) + int(duration_hour)
    total_minutes = int(starting_minute) + int(duration_minute)
    days = 0

    # using floor division to calculate the number of hours
    # using the remainder operator to find the number of minutes
    if total_minutes >= 60:
        total_hours += total_minutes // 60
        total_minutes %= 60
    if total_minutes < 10:
        total_minutes = "0" + str(total_minutes)
    else:
        pass

    while total_hours >= 24:
        total_hours = total_hours - 24
        days += 1

    if time_of_day == 'AM':
        if int(total_hours) >= 12:
            time_of_day = 'PM'
    elif time_of_day == 'PM':
        if int(total_hours) >= 12:
            time_of_day = "AM"
            days += 1 
    else:
        pass

    if total_hours > 12:
        total_hours -= 12
    elif total_hours == 0 and time_of_day == AM: 
        total_hours = 12
    else:
        pass

    next_day = ""
    if days == 1:
        next_day = "(next day)"
    elif days > 1:
        next_day = "({0} days later)".format(days)
    else:
        next_day = ""

    if day:
        day = day.lower()
        if day == "monday":
            day = 1
        elif day == "tuesday":
            day = 2
        elif day == "wednesday":
            day = 3
        elif day == "thursday":
            day = 4
        elif day == "friday":
            day = 5
        elif day == "saturday":
            day = 6
        elif day == "sunday":
            day = 7
        else:
            day = None

        sum_day_s = day + days
        if sum_day_s <= 7:
            day = sum_day_s
        else:
            day = sum_day_s % 7

        if day == 1:
            day = "Monday"
        elif day == 2:
            day = "Tuesday"
        elif day == 3:
            day = "Wednesday"
        elif day == 4:
            day = "Thursday"
        elif day == 5:
            day = "Friday"
        elif day == 6:
            day = "Saturday"
        elif day == 7:
            day = "Sunday"

        new_time = "{0}:{1} {2}, {3} {4}".format(str(total_hours),str(total_minutes),str(time_of_day),str(day),str(next_day))

    elif day and next_day == "":
        new_time = "{0}:{1} {2}, {3}".format(str(total_hours),str(total_minutes),str(time_of_day),str(day))

    elif not day and next_day == "":
        new_time = "{0}:{1} {2}".format(str(total_hours),str(total_minutes),str(time_of_day))
    
    else:
        new_time = "{0}:{1} {2} {3}".format(str(total_hours),str(total_minutes),str(time_of_day),str(next_day))

    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/121.0.0.0 Safari/537.36

Challenge Information:

Scientific Computing with Python Projects - Time Calculator

Try going through code from the start (or end), and modify in some way (it could be even adding more spaces) the part that you think is returned for this case. If you modify it, but output doesn’t change it means for one or another reason, that’s not the part of the output that’s returned for this case.

1 Like

Oh that was great advice! Thanks so much! I figured it out.

1 Like

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