Build a Time Calculator Project - Build a Time Calculator Project

def add_time(start, duration, day = 0):
    day_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    new_time_of_day = ['AM', 'PM']
    current_day = 0
    inital_hours = int(start.split()[0].split(":")[0])
    initial_minutes = int(start.split()[0].split(":")[1])
    initial_time_of_day = start.split()[1]
    added_hours = int(duration.split(":")[0])
    added_minutes = int(duration.split(":")[1])
    new_minutes = initial_minutes + added_minutes


    if new_minutes >= 60:
        added_hours += new_minutes // 60
        new_minutes = new_minutes  % 60
    
    if new_minutes < 10:
        new_minutes = f'{new_minutes:02d}'
        

    new_hours = (inital_hours + added_hours) % 12

    if new_hours == 0:
        new_hours = 12


    if initial_time_of_day == "PM":
        current_day += ((inital_hours + 12 + added_hours) // 24)
    else:
        current_day += ((inital_hours + added_hours) // 24) 

    if day != 0:
        for i in day_of_week:
            if i == day.capitalize():
                new_day = day_of_week.index(i) + current_day

        if new_day > 7:
            new_day = new_day % 7
    else:
        new_day = ""


    if current_day == 0:
        current_day = ''
    elif current_day == 1:
        current_day = ' (next day)'
    else:
        current_day = f' ({current_day} days later)'

    if day != 0:
        if ((inital_hours + added_hours) // 12) % 2 == 1:
            new_time_of_day.remove(initial_time_of_day)
            new_time = print(f'{new_hours}:{new_minutes} {new_time_of_day[0]}, {day_of_week[new_day]}{current_day}')
        else:
            new_time = print(f'{new_hours}:{new_minutes} {initial_time_of_day}, {day_of_week[new_day]}{current_day}')
    else:
        if ((inital_hours + added_hours) // 12) % 2 == 1:
            new_time_of_day.remove(initial_time_of_day)
            new_time = print(f'{new_hours}:{new_minutes} {new_time_of_day[0]}{current_day}')
        else:
            new_time = print(f'{new_hours}:{new_minutes} {initial_time_of_day}{current_day}')        

    return new_time


add_time('3:30 PM', '2:12')
add_time('11:55 AM', '3:12')
add_time('2:59 AM', '24:00')
add_time('11:59 PM', '24:05')
add_time('8:16 PM', '466:02', 'tuesday')
add_time('11:59 PM', '0:00')
add_time('3:30 PM', '2:12', 'Monday')
add_time('2:59 AM', '24:00', 'saturDay')
add_time('11:59 PM', '24:05', 'Wednesday')
add_time('8:16 PM', '466:02', 'tuesday')

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!


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

Somehow I did not post my description of problem. In short, code seems to work but it does not pass test.

open the browser console with F12, see there the more detailed output of the tests

also you could try print("Output of the function", add_time('3:30 PM', '2:12'))

I mean, output on the F12 console looks the same. And print("Output of the function", add_time('3:30 PM', '2:12')) is no help.

I mean I’m happy without passing the tests but I just wonder if I actually did something wrong nevertheless the output looks same.

I guess that means you didn’t try it because if you try this the problem will become immediately obvious.

Generally, for all of these projects you should be calling the function like this

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

And not using print() from inside the function except for troubleshooting purposes. You need to find out what your function is returning by return

Thx a lot. Solved. Noob mistake I guess.

1 Like