Tell us what’s happening:
Can someone check my code and see why it isn’t working?
Your code so far
def add_time(start, duration, day_of_week = False):
days_of_the_week_index = {'Monday': 0, 'Tuesday': 1, 'Wednesday': 2, 'Thursday': 3, 'Friday': 4, 'Saturday': 5, 'Sunday': 6}
days_of_the_week_array = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
duration_tuple = duration.partition(':')
duration_hours = int(duration_tuple[0])
duration_minutes = int(duration_tuple[2])
start_tuple = start.partition(':')
start_minutes_tuple = start_tuple[2].partition(' ')
start_hours = int(start_tuple[0])
start_minutes = int(start_tuple[0])
am_or_pm = start_minutes_tuple[2]
am_and_pm_flip = {'AM': 'PM', 'PM': 'PM'}
amount_of_days = int((duration_hours) / 24)
end_minutes = start_minutes + duration_minutes
if end_minutes >= 60:
start_hours += 1
end_minutes = end_minutes % 60
amount_of_am_pm_flips = int((start_hours + duration_hours) / 12)
end_hours = start_hours + duration_hours % 12
end_minutes = end_minutes if end_minutes > 9 else '0' + str(end_minutes)
end_hours = end_hours = 12 if end_hours == 0 else end_hours
if am_or_pm == 'PM' and start_hours + (duration_hours % 12) >= 12:
amount_of_days += 1
am_or_pm = am_and_pm_flip[am_or_pm] if amount_of_am_pm_flips % 2 == 1 else am_or_pm
returnTime = str(end_hours) + ':' + str(end_minutes) + ' ' + am_or_pm
if day_of_week:
index = int((day_of_week_index[day_of_week]) + amount_of_days) % 7
new_day = days_of_the_week_array[index]
returnTime += ', '+ new_day
if amount_of_days == 1:
return returnTime + ' ' + '(next day)'
elif amount_of_days > 1:
return returnTime + ' (' + str(amount_of_days) + 'days later)'
return returnTime
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Build a Time Calculator Project - Build a Time Calculator Project