Tell us what’s happening:
I can’t tell what’s wrong with the code. It seems to be passing the tests when I run them but the code results say otherwise. Could someone help point me in the right direction?
Your code so far
def add_time(start, duration, day=None):
# Declare variables
start_hours = int(start.split(':')[0])
start_minutes = int(start.split(':')[1][:2])
start_meridian = start.split(':')[1][3:]
duration_hours = int(duration.split(':')[0])
duration_minutes = int(duration.split(':')[1])
# Convert to 24 hour format
if start_meridian == 'PM' and start_hours != 12:
start_hours += 12
elif start_meridian == 'AM' and start_hours == 12:
start_hours += 0
#Add duration hours and minutes
total_hours = start_hours + duration_hours
total_minutes = start_minutes + duration_minutes
#Handle overflow of minutes
if total_minutes >= 60:
total_hours += 1
total_minutes %= 60
#Calculate days later
days_later = total_hours // 24
hours_later = total_hours % 24
#Convert back to 12-hour format
if hours_later == 0 :
hours_later = 12
meridian ='AM'
elif hours_later < 12 :
meridian = 'AM'
elif hours_later > 12 :
meridian = 'PM'
hours_later -= 12
# Handle the optional days of the week
if day :
days_of_the_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
day_index = days_of_the_week.index(day.capitalize())
end_day_index = (day_index + days_later) % 7
end_day = days_of_the_week[end_day_index]
else:
end_day = None
# Format the final time
formatted_time = f'{hours_later}:{total_minutes:02} {meridian}'
if end_day:
formatted_time += f', {end_day}'
if days_later == 1:
formatted_time += f' (next day)'
elif days_later > 1:
formatted_time += f' ({days_later} days later)'
print (formatted_time)
#Handle next day or days later message
#Return the fully formated time
return
add_time('2:59 AM', '24:00', 'saturDay')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Challenge Information:
Build a Time Calculator Project - Build a Time Calculator Project