Tell us what’s happening:
that’s my output:
5:42 PM
3:07 PM
2:45 AM (next day)
12:05 AM
2:59 PM (next day)
12:04 AM (2 days later)
6:18 AM (20 days later)
5:01 AM
5:42 PM, Monday
2:59 PM, Sunday (next day)
12:04 AM, Friday (2 days later)
6:18 AM, Monday (20 days later)
and that is the expected output:
“5:42 PM”
“3:07 PM”
“2:45 AM (next day)”
“12:05 PM”
“2:59 AM (next day)”
“12:04 AM (2 days later)”
“6:18 AM (20 days later)”
“5:01 AM”
“5:42 PM, Monday”
“2:59 AM, Sunday (next day)”
“12:04 AM, Friday (2 days later)”
“6:18 AM, Monday (20 days later)”
can someone give me a hint to figure out why sometimes AM don’t change to PM and viceversa?
Your code so far
def add_time(start, duration, day = None):
starting_hour, form = start.split()
start_hrs, start_min = starting_hour.split(':')
dur_hrs, dur_min = duration.split(':')
total_hrs = int(start_hrs) + int(dur_hrs)
total_min = int(start_min) + int(dur_min)
day_ = 0
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
day_ = total_hrs / 24
if day_ < 1:
day_ = 0
if day_ > 1:
day_ = int(day_)
if total_min > 59:
total_hrs += 1
total_min -= 60
if total_hrs > 12:
total_hrs %= 12
if total_hrs == 0:
total_hrs = 12
if form == 'AM':
form = 'PM'
else:
form = 'AM'
day_ += 1
time = f"{total_hrs}:{str(total_min).zfill(2)} {form}"
if day:
current_day_index = days.index(day.title())
next_day_index = int((current_day_index + day_) % 7)
time += f", {days[next_day_index]}"
if day_ == 1:
time += " (next day)"
if day_ > 1:
time += f" ({int(day_)} days later)"
return time
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
.
Challenge: Time Calculator
Link to the challenge: