Build a Time Calculator Project

Hi,
Why doesn’t my code pass?

def add_time(start, duration):

datas = {'h_s': int(start.split(':')[0]), 'm_s': int(start.split()[0].split(':')[1]), 'h_d': int(duration.split(':')[0]), 'm_d': int(duration.split(':')[1]), 'am_pm': start.split()[1]}

hour_start = datas['h_s']
hour_duration = datas['h_d']
min_start = datas['m_s']
min_duration = datas['m_d']
sign = datas['am_pm']
sign_list = []

if sign == 'AM':
    sign_list.append(sign)
    sign_list.append('PM')
else:
    sign_list.append(sign)
    sign_list.append('AM')

def calc_h(a, b):
    while b == 0:
        return a
    else:
        return calc_h(a + 1, b - 1)

a = calc_h(hour_start, hour_duration) // 12
hour_new = calc_h(hour_start, hour_duration) - (12 * a)
b = calc_h(min_start, min_duration) // 60
hour_new = hour_new + b
minutes_new = calc_h(min_start, min_duration) - (60 * b)

if a + b == 0:
    sign_new = sign_list[0]
elif (a + b) % 2 == 1:
    sign_new = sign_list[1]
elif a + b == 2:
    sign_new = sign_list[1]
else:
    sign_new = sign_list[0]

days = a // 2
if sign == "PM" and a > 0:
    days += 1

if days == 1:
    text = '(next day)'
elif days >= 2:
    text = f'({str(days)} days later)'
else:
    text = ''

if len(str(minutes_new)) < 2:
    new_time = print(f'{hour_new}:0{minutes_new} {sign_new} {text}')
else:
    new_time = print(f'{hour_new}:{minutes_new} {sign_new} {text}')

return new_time

add_time(‘3:30 PM’, ‘2:12’)

What your function returns?

My function respond 5:42 PM and all rest of examples are correct. Only what i don’t have yet is a day name changer.

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge. This button only appears if you have tried to submit an answer at least three times.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

1 Like

I was tried but it didn’t work like before so i decided to make topic like you see. When I clicked “Submit” my window just disappeared and topic wasn’t created.

What do you mean respond? You can wrap the function call with the print() to see what is returned.

When i printed function I have: 5:42 PM
None

Then None is what function returns, and the 5:42 PM is what function prints on it own. add_time is expected to return the string with time.

1 Like

Great! thank you - it’s working now :raised_hands: