Time Calculator

Tell us what’s happening:

My code works but its not being accepted :frowning:

Your code so far

def add_time(start, duration, start_day=None):
    start_time, period = start.split( )
    start_hour, start_minute = map(int, start_time.split(':'))
    duration_hour, duration_minute = map(int, duration.split(':'))    
    if period=='PM':
        start_hour+=12
    total_minutes = (start_hour + duration_hour) * 60 + start_minute + duration_minute
    n = total_minutes // (24 * 60)
    new_hour = total_minutes // 60 - n * 24
    new_minute = total_minutes % 60
    if new_hour <= 12:    
        new_period = "AM"
        if new_hour == 0:
            new_hour = 12
    else:
        new_period = "PM"
        new_hour -= 12    
    new_time = f"{new_hour:02d}:{new_minute:02d} {new_period}"   
    if start_day:
        days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
        start_day = start_day.capitalize()
        start_index = days_of_week.index(start_day)
        new_index = (start_index + n) % 7
        new_day = days_of_week[new_index]
        new_time += f", {new_day}"
    if n == 1:
        new_time += " (next day)"
    elif n > 1:
        new_time += f" ({n} days later)"
    return new_time

print(add_time("3:30 PM", "2:12"))
# should return 5:42 PM.

print(add_time("11:55 AM", "3:12"))
# should return 3:07 PM.

print(add_time("2:59 AM", "24:00"))
# should return 2:59 AM.

print(add_time("11:59 PM", "24:05"))
# should return 12:04 AM (2 days later).

print(add_time("8:16 PM", "466:02"))
# should return 6:18 AM (20 days later).

print(add_time("3:30 PM", "2:12", "Monday"))
# should return 5:42 PM, Monday.

print(add_time("2:59 AM", "24:00", "saturDay")) 
# should return 2:59 AM, Sunday (next day).

print(add_time("11:59 PM", "24:05", "Wednesday") )
# should return "12:04 AM, Friday (2 days later)".

print(add_time("8:16 PM", "466:02", "tuesday") )
# should return 6:18 AM, Monday (20 days later).

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 (').

1 Like

Which tests are failing? What is the output of the test suite?

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

Are you returning exactly that string?

all but two calls:
Calling add_time("11:59 PM", "24:05") should return 12:04 AM (2 days later) .
Calling add_time("11:59 PM", "24:05", "Wednesday") should return "12:04 AM, Friday (2 days later)" .

these are the outputs:

05:42 PM
03:07 PM
02:59 AM (next day)
12:04 AM (2 days later)
06:18 AM (20 days later)
05:42 PM, Monday
02:59 AM, Sunday (next day)
12:04 AM, Friday (2 days later)
06:18 AM, Monday (20 days later)

What does the test suite say though? You should have a diff in the console output

No, no diff with the projects in the freeCodeCamp page

I ask again if you haven’t seen my post

I don’t get what you mean with the diff

You should have a bunch of output in the console. Most of that output is called a ‘diff’.

No, these are my outputs:

05:42 PM
03:07 PM
02:59 AM (next day)
12:04 AM (2 days later)
06:18 AM (20 days later)
05:42 PM, Monday
02:59 AM, Sunday (next day)
12:04 AM, Friday (2 days later)
06:18 AM, Monday (20 days later)

if it’s not exactly the requested string, that’s why it doesn’t pass

That’s not the test suite output. What is in the console on the right side of the repl screen?

Jeremy, no replit anymore

you mean this ones that should be checked, right?

  • Calling add_time("3:30 PM", "2:12") should return 5:42 PM.

  • Failed:Calling add_time("11:55 AM", "3:12") should return 3:07 PM.

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

  • Failed:Expected period to change from AM to PM at 12:00.

  • Failed:Calling add_time("2:59 AM", "24:00") should return 2:59 AM.

  • Passed:Calling add_time("11:59 PM", "24:05") should return 12:04 AM (2 days later).

  • Failed:Calling add_time("8:16 PM", "466:02") should return 6:18 AM (20 days later).

  • Failed:Expected adding 0:00 to return the initial time.

  • Failed:Calling add_time("3:30 PM", "2:12", "Monday")should return 5:42 PM, Monday.

  • Failed:Calling add_time("2:59 AM", "24:00", "saturDay") should return 2:59 AM, Sunday (next day).

  • Passed:Calling add_time("11:59 PM", "24:05", "Wednesday") should return "12:04 AM, Friday (2 days later)".

  • Failed:Calling add_time("8:16 PM", "466:02", "tuesday") should return 6:18 AM, Monday (20 days later).

I thought repl was only removed for the backend challenges?

Ahh, I see. A link to the project would have helped here to see which version this is.

for everything, now there this: https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/time-calculator

So look at this. Run this exact test case. What exactly do you return from your code?

02:59 AM, Sunday (next day)

That’s not exactly, perfectly, completely identical to what the test says, which is why I wanted you to look at it again instead of quoting it at me :wink:

You have a single character that should not be there.

1 Like