Build a Time Calculator Project - Build a Time Calculator Project

Tell us what’s happening:

those two tests failed
3. Expected time to end with ‘(next day)’ when it is the next day.
8. Expected adding 0:00 to return the initial time.

Your code so far

days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
def add_time(start, duration,day=''):
    h = int(start.split()[0].split(':')[0])
    m = int(start.split()[0].split(':')[1])
    timing = start.split()[1]
    h_added = int(duration.split(':')[0])
    m_added = int(duration.split(':')[1])

    new_h = h + h_added 
    new_m = m + m_added
    new_timing = timing
    days = 0
    if new_m >=60:
        new_m %= 60
        if new_m <10:
            new_m = '0' + str(new_m)
        new_h += 1
    if new_h >=12 :
        new_h %= 12 
        days = int((h + h_added) / 24) 
        if new_h == 0 :
            new_h = 12
        if not days <1 and new_timing == 'PM':
            days +=1
        if days % 2 ==0 :
            if new_timing == 'AM':
                new_timing = 'PM'
            else :
                new_timing = 'AM'
        
    
    print_sentense = f'{new_h}:{new_m} {new_timing}'
    if day :
        calc = days % 7
        if calc == 0 :
            print_sentense += f', {day.capitalize()}'
        if calc > 0 :
            new_day_index = (days_of_week.index(day.capitalize())+ calc) % 7
            print_sentense += f', {days_of_week[new_day_index]}'

    if days ==1 :
        print_sentense += ' (next day)'
    
    if days > 1 :
        print_sentense += f' ({days} days later)'





    

    return print_sentense

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

Your browser information:

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

Challenge Information:

Build a Time Calculator Project - Build a Time Calculator Project

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!

If I execute your code by print(add_time('3:30 PM', '9:30')),
it would give out 1:00 AM without “next day”.

For print(add_time('12:00 PM', '0:00')),
it gives out 12:0 AM

You may use pythontutor.com to run your code and check the values of each variable to trace what’s wrong.

Generally speaking, it seems that your blocks of conditional statements fall short to deal with certain scenarios. You may consider turning the starting time to 24 hours format before adding, that may save lines of conditional statements dealing with am/pm issues and make the code simpler.