Build a Time Calculator Project - Build a Time Calculator Project

I do not see that issue with your code, but the last code you posted was 9 days ago, did you make any changes since then? can you post your updated code?

No, here is my code as it stands right now:

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

Hi @jcant

See if you can fix this case:

image

Happy coding

that is what i am confused about… because my return statement for this is:

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

and next day is stored in new day.
this is what i don’t understand

What happens when you add a single space between these two variables?:

it messes up the other tests

You know what the problem is, you just need to fix it.

Code the output so that when “next day” is needed, it has a space in front of it, and when it’s not, you don’t add a trailing space.

{new_m} {new_day}'

If {new_day} is empty then your string will end with a space, which is not wanted.

when I do this it fails the other tests, so that doesn’t work

Hi @jcant

You will need to fix up the other tests that were previously passing.

When you do what? You’ll need to try something different then.

yes, that is why i am here asking for help. i don’t know what to do and am learning this. i have tried finding the answers on my own with no luck

At the moment your code is producing this result:

image

When using the following code:

Is there another way you could approach the problem?

figured out my issue. thanks

1 Like