Tell us what’s happening:
I am working on the time calculator project. When I call the function to test the output for each problem, it is showing the expected output, however, when I run the tests, they all come up as failed. Can anyone help?
Thanks!
Your code so far
def add_time(start, duration, day = None):
if day:
day_formatted = str(day).title()
index_of_the_week = {'Monday':1, 'Tuesday':2, 'Wednesday':3, 'Thursday':4, 'Friday':5, 'Saturday':6, 'Sunday':7}
days_of_the_week = {1:'Monday', 2:'Tuesday', 3:'Wednesday', 4:'Thursday', 5:'Friday', 6:'Saturday', 7:'Sunday'}
day_index = index_of_the_week[day_formatted]
start_time, ampm = start.split(' ')
s_hours, s_minutes = start_time.split(':')
if ampm == 'PM':
modified_hours = int(s_hours) + 12
else:
modified_hours = int(s_hours)
d_hours, d_minutes = duration.split(':')
total_minutes = int(s_minutes) + int(d_minutes)
extra_hours = total_minutes // 60
final_minutes = total_minutes % 60
total_hours = modified_hours + int(d_hours) + extra_hours
extra_days = total_hours // 24
final_hours = total_hours % 24
if final_hours >= 12:
format_hours = 'PM'
final_hours -= 12
else:
format_hours = 'AM'
if final_hours == 0:
final_hours += 12
if final_minutes < 10:
final_minutes = '0'+str(final_minutes)
if extra_days == 1:
days_later = ' (next day)'
elif extra_days == 0:
days_later = ''
else:
days_later = ' (' + str(extra_days) + ' days later)'
if day:
if extra_days > 0:
day_index += extra_days
if day_index <= 7:
final_day = days_of_the_week[day_index]
else:
while day_index > 7:
day_index -= 7
final_day = days_of_the_week[day_index]
else:
final_day = day_formatted
if day:
if days_later != '':
new_time = print(f'{str(final_hours)}:{str(final_minutes)} {format_hours}, {final_day}{days_later}')
else:
new_time = print(f'{str(final_hours)}:{str(final_minutes)} {format_hours}, {final_day}')
elif days_later != '':
new_time = print(f'{str(final_hours)}:{str(final_minutes)} {format_hours}{days_later}')
else:
new_time = print(f'{str(final_hours)}:{str(final_minutes)} {format_hours}')
return new_time
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Challenge Information:
Build a Time Calculator Project - Build a Time Calculator Project