Build a Time Calculator Project - Build a Time Calculator Project

Tell us what’s happening:

Not sure where I am going wrong. The correct time is returned in the console, but all of my tests are failing

Your code so far

def add_time(start, duration, day_of_week=''):
    start_m = start[-2:]  # Extract AM/PM
    start_hour = int(start.split(':')[0])

    if start_hour == 12 and start_m == 'AM':
        start_hour = 0
    elif start_hour == 12 and start_m == 'PM':
        start_hour = 12
    elif start_m == 'PM':
        start_hour += 12
    
    start_min = int(start[-5:-3])
    duration_hour = 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 = ''

    new_min = start_min + duration_min
    if new_min >= 60:
        duration_hour = duration_hour + 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}'

    new_hour = start_hour + duration_hour
    if new_hour >= 24:
        extra_day = new_hour // 24
        new_hour %= 24
    elif new_hour < 12:
        new_m = 'AM'
    elif new_hour >= 12 and new_hour < 24:
        new_m = 'PM'
        new_hour -= 12
    if new_hour == 0:
        new_hour = 12

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

    
    return new_time

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

Your browser information:

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

Challenge Information:

Build a Time Calculator Project - Build a Time Calculator Project

@jcant It looks like there are spacing issues in your output. Please ensure that your output matches the formatting specified in the instructions exactly.

thank you, I partly solved the problem. I stored ‘AM’ and ‘PM’ in the variable new_m and it is in the return statement but it isn’t showing in the console and I am not sure why

@jcant I see that you are using if-elif statements.

Keep in mind that when an if condition evaluates to True, the following elif statements will not be executed.

I assume you are trying to get '12:03 AM, Thursday (2 days later)' as the output, but it never returns AM because the second elif condition is never reached.

Try updating your if-elif structure to use separate if statements instead, so all necessary conditions are checked.

Yes, thank you. That worked to display ‘AM’ or ‘PM’, but now i have a new problem. My spacing is off when the timer goes into a new day. It returns ‘6:18 AM(20 days later’ and when i get the spacing right on this test, the other ones fail. I’ve tried searching this issue, but I am not finding anything that works.

Hi @jcant

To help you debug, you could try commenting out the more complex code.
When you get the spacing right for the first test, uncomment the next part of the code - (next day).

Work on it until the code passes for both conditions. Then repeat the process. This will avoid the pains of going from passing one test, changing code, and failing test or tests which were previously passing.

Happy coding

hey, i’m not sure what part of the code you are recommending to comment out because my only problem seems to be the return statements, and it won’t work if i comment those out. (I have also tried all sorts of various spacing on these return statements.) I commented out the extra_day variable, but the tests that pass those aren’t affected by that anyway so it doesn’t change anything.

@jcant What’s your current output with your latest fix? Sharing it will help us guide you in the right direction more efficiently.

They all pass except for the ones that require ‘x days later.’ for example: 'print(repr(add_time('2:59 AM', '24:00')))' gives the output, '2:59 AM(next day)'

here are my return statements:

if day_of_week == '':
        new_time = f'{new_hour}:{new_min} {new_m}{new_day}'
    else:
        new_time = f'{new_hour}:{new_min} {new_m}, {new_day_of_week}{new_day}'

and if i fix the spacing on this test, some of the others fail

Compare your output to the expected output in the tests.

  1. Calling add_time('2:59 AM', '24:00') should return '2:59 AM (next day)'.
print(repr(add_time('2:59 AM', '24:00')))
'2:59 (next day)'

It’s missing AM.

I added the latest return statements to your original code. If it’s changed please provide your full updated code to test.

No, i fixed AM and PM, but i can’t get the spacing right. They all pass except for the ones that require ‘x days later.’ for example: 'print(repr(add_time('2:59 AM', '24:00')))' gives the output, '2:59 AM(next day)'

here are my return statements:

if day_of_week == '':
        new_time = f'{new_hour}:{new_min} {new_m}{new_day}'
    else:
        new_time = f'{new_hour}:{new_min} {new_m}, {new_day_of_week}{new_day}'

and if i fix the spacing on this test, some of the others fail

You’ll need to post your full updated code, but:

Since your output does not match the test output:
'2:59 AM(next day)'
'2:59 AM (next day)'

You’re saying if you add a space and it breaks other tests.

If you add a space like this: '2:59 AM (next day)' Does it then leave a trailing space when the next day isn’t needed, like this? '2:59 AM '

That extra space at the end would be a problem.

this is my full code:

def add_time(start, duration, day_of_week=''):
    start_m = start[-2:]  # Extract AM/PM
    start_hour = int(start.split(':')[0])

    if start_hour == 12 and start_m == 'AM':
        start_hour = 0
    if start_hour == 12 and start_m == 'PM':
        start_hour = 12
    if start_m == 'PM':
        start_hour += 12
    
    start_min = int(start[-5:-3])
    duration_hour = 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 = ''

    new_min = start_min + duration_min
    if new_min >= 60:
        duration_hour = duration_hour + new_min // 60
        new_min %= 60
    if new_min == 0:
        new_min = '00'
    if new_min > 0 and new_min < 10:
        new_min = f'0{new_min}'

    new_hour = start_hour + duration_hour
    if new_hour >= 24:
        extra_day = new_hour // 24
        new_hour %= 24
    if new_hour < 12:
        new_m = 'AM'
    if new_hour >= 12 and new_hour < 24:
        new_m = 'PM'
        new_hour -= 12
    if new_hour == 0:
        new_hour = 12

    if day_of_week != '':
        day_of_week = day_of_week.lower()
        day_index = week.index(day_of_week)
        new_day_of_week = week[(day_index + extra_day) % 7].capitalize()
    
    if extra_day == 1:
        new_day = '(next day)'
    if extra_day > 1:
        new_day = f'({extra_day} days later)'
    
    if day_of_week == '':
        new_time = f'{new_hour}:{new_min} {new_m}{new_day}'
    else:
        new_time = f'{new_hour}:{new_min} {new_m}, {new_day_of_week} {new_day}'

    
    return new_time

print(repr(add_time('3:30 PM', '2:12')))
print(repr(add_time('8:16 PM', '466:02', 'tuesday') ))

the tests that don’t pass are: Calling add_time('2:59 AM', '24:00') should return '2:59 AM (next day)', etc. and Calling add_time('3:30 PM', '2:12', 'Monday') should return '5:42 PM, Monday'. The out put right now shows, '‘2:59 AM(next day)’ and if i change the spacing on this test, the others that pass right now will fail.

Since your output does not match the test output:
'2:59 AM(next day)'
'2:59 AM (next day)'

You’re saying if you add a space and it breaks other tests.

If you add a space like this: '2:59 AM (next day)' Does it then leave a trailing space when the next day isn’t needed, like this? '2:59 AM '

That extra space at the end would be a problem. Is that what happens?

sorry for the late reply, but yes. That is what happens

Does that help you to find the problem? You need to find where that space gets added, or remove it at the end or something.

that’s the part i’m confused about. When it take the space away, the other tests fail. I am not sure what I am doing wrong. (also, i haven’t changed my code from the previous message)

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

Now this is what I get with that most recent block of code:

print(repr(add_time('2:59 AM', '24:00')))

'2:59 (next day)'

5. Calling add_time('2:59 AM', '24:00') should return '2:59 AM (next day)'

Where did AM go? The spacing looks ok you just need the AM to pass this test.

but AM is stored in variable new_m which is in both return statements.