Tell us what’s happening:
Hello all! I’ve finished my code for the project and everything seems to run ok. When I run the tests, some I pass and some not; however, I’ve tested the failed ones manually and I seem to obtain the expected output. Also, I’ve checked that I am returning a string and not a print statement. I could not find a similar problem in the forums.
Would somebody be so kind to help me here? Thank you very much and happy coding!
Your code so far
def add_time(start, duration, day = ""):
# Set a list with all days of the week
days_of_week = ("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" )
# The approach will be to compute the start hour and the duration to minutes
# Turn the input into a list with three items: hour, minutes and AM/PM
start_list = start.replace(":"," ").split()
# Sum the hours and the minutes
start_in_minutes= int(start_list[0])*60 + int(start_list[1])
# Add 12 hours if it's PM
if start_list[2] == "PM":
start_in_minutes += 12*60
# Now do the same with duration
duration_list = duration.split(':')
duration_in_minutes = int(duration_list[0])*60 + int(duration_list[1])
#Now we can add both durations
total_time_minutes = start_in_minutes + duration_in_minutes
# We obtain a number that we can split into days, hours and minutes using // and %
days = total_time_minutes // (24*60)
days_modulus = total_time_minutes % (24*60)
hours = days_modulus // 60
minutes = days_modulus % 60
# Now that we have the amount of days , hours and minutes transcurred, we can start printing
#For days
days_string = ""
if days > 1:
days_string = f"({days} days later)"
elif days == 1:
days_string = f"(next day)"
#For hours and minutes
time_string = ""
if hours > 12:
time_string = f"{hours-12}:{minutes:02d} PM"
elif hours == 12:
time_string = f"{hours}:{minutes :02d} PM"
elif hours == 0:
time_string = f"12:{minutes :02d} AM"
else:
time_string = f"{hours}:{minutes :02d} AM"
#For week days
next_week_day_string = ""
if day:
week_day_index = days_of_week.index(day.lower())
next_week_day_index = (week_day_index + days) % 7
next_week_day_string = f", {days_of_week[next_week_day_index].capitalize()}"
# Finally, print
return f"{time_string}{next_week_day_string} {days_string}"
print(add_time('3:30 PM', '2:12', 'Monday'))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0
Challenge Information:
Build a Time Calculator Project - Build a Time Calculator Project