Scientific Computing with Python Projects - Time Calculator

In VS Code I pass all the test cases. What is wrong?

def add_time(start, duration, day = None):

    start_time, period = start.split()
    start_hour, start_minute = map(int, start_time.split(':'))

    duration_hour, duration_minute = map(int, duration.split(':'))

    #Calculate Total Mins

    TotalMins = start_hour * 60 + start_minute
    TotalMins += duration_hour * 60  + duration_minute

    #Calculate New Time

    NewHour = TotalMins // 60 % 12
    NewMin = TotalMins % 60
    new_period = period

    #calculate period
    days_later = TotalMins // (60 * 24)

    #if TotalMins // 60 >= 12:
        #new_period = 'PM' if period == 'AM' else 'AM'
        #if new_period ==  'AM':
         #   days_later = 1 

    periods_passed = 0
    if TotalMins % (12*60) >= 1:
        periods_passed = TotalMins // (12*60)     
    
    if periods_passed % 2 != 0:
        if period == "AM":
            new_period = "PM"
        else:
            new_period = "AM"
    
    if period == "PM" and periods_passed % 2 != 0:
        days_passed = (periods_passed // 2) + 1
    else:
        days_passed = periods_passed // 2

    #Calculate Days later

    if days_passed == 1:
        days_later = '(the next day)'
    elif days_passed > 1:
        days_later = f'{days_passed} days later'

    if (NewHour > 12):
        NewHour = NewHour - 12
    # Format the new time
    new_time = f"{NewHour if NewHour != 0 else 12}:{NewMin:02d} {new_period}"
   
    # Add 'next day' or 'n days later' if applicable
    if days_passed == 1:
        new_time =  f"{NewHour if NewHour != 0 else 12}:{NewMin:02d} {new_period} {days_later}"
    if days_passed > 1:
        new_time =  f"{NewHour if NewHour != 0 else 12}:{NewMin:02d} {new_period} ({days_passed} days later)"
    if day:
        days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
        start_day_index = days.index(day.lower().capitalize())
        new_day_index = (start_day_index + days_passed) % 7
        new_day = days[new_day_index]
        new_time =  f"{NewHour if NewHour != 0 else 12}:{NewMin:02d} {new_period}, {new_day}"
        if days_passed == 1:
            new_time =  f"{NewHour if NewHour != 0 else 12}:{NewMin:02d} {new_period}, {new_day} {days_later}"
        if days_passed > 1:
            new_time =  f"{NewHour if NewHour != 0 else 12}:{NewMin:02d} {new_period}, {new_day} ({days_passed} days later)"

    return new_time

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

when you ask for help please include also the link to the project

You are failing

Failed:Expected time to end with '(next day)' when it is the next day.

What is your output? Is it exactly like that?

I’m not sure it is

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