Tell us what’s happening:
Some parts of my code are not working for example when i try to print the result of:‘11:55 AM’, ‘3:12’ it doesn’t work, could anyone help me to figure this out please. I have already spend a couple of days working on this one.
Your code so far
def add_time(start, duration, day_week=None):
days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
startHour, start_period_minute = start.split(':')
startMinute, period = start_period_minute.split()
start_hour = int(startHour)
start_minute = int(startMinute)
durat_time = duration.split(':')
durhour = durat_time[0]
durminute = durat_time[1]
dur_hour = int(durhour)
dur_minute = int(durminute)
#calculate the number of total minutes
total_minutes = start_minute + dur_minute
extra_hour = total_minutes // 60
remaining_minutes = total_minutes % 60
#calculate the number of total hours
total_hours = extra_hour + dur_hour + start_hour
#convert the start hour to 24 hours format
if period == "PM" and start_hour != 12:
start_hour += 12
elif period == "AM" and start_hour == 12:
start_hour = 0
#calculate the number of days from total_hours
number_of_days = total_hours // 24
remaining_hour = total_hours % 24
#convert the remaining hours to 12 hours format
if remaining_hour >= 12:
new_period = "PM"
elif remaining_hour > 12:
remaining_hour -= 12
else:
new_period = "AM"
if remaining_hour == 0:
remaining_hour = 12
if day_week:
current_day = days_of_week.index(day_week.capitalize())
next_day_index = (current_day + number_of_days) % 7
new_day = days_of_week[next_day_index]
new_day_time = f"{remaining_hour}:{remaining_minutes:02d} {new_period}, {new_day}"
else:
new_day_time = f"{remaining_hour}:{remaining_minutes:02d} {new_period}"
if number_of_days > 0:
if number_of_days == 1:
new_day_time += " (next day)"
else:
new_day_time += f" ({number_of_days} days later)"
return new_day_time
new_time = add_time('11:55 AM', '3:12')
print(new_time)
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