Tell us what’s happening:
Hi!
I have just completed the Time Calculator project. All results are as expected, but none of the tests will pass.
Any ideas?
Your code so far
```py
import re
def add_time(start, duration, day = None):
start_hour = re.findall("[0-9]+:", start)[0][:-1]
start_minute = re.findall(":[0-9]+", start)[0][1:]
duration_hour = re.findall("[0-9]+:", duration)[0][:-1]
duration_minute = re.findall(":[0-9]+", duration)[0][1:]
hours_sum = int(start_hour) + int(duration_hour)
minutes_sum = int(start_minute) + int(duration_minute)
start_period = re.findall("AM|PM", start, re.IGNORECASE)[0].upper()
# convert hours
if minutes_sum < 60:
new_hour = hours_sum % 12
else:
new_hour = (hours_sum + 1) % 12
if new_hour == 0:
new_hour = "12"
# convert minutes
new_minute = (minutes_sum) % 60
if new_minute < 10:
new_minute = f"0{new_minute}"
# swap period when necessary
if (hours_sum % 12) == 11:
if minutes_sum >= 60:
periods_passed = (hours_sum // 12) + 1
else:
periods_passed = hours_sum // 12
else:
periods_passed = hours_sum // 12
if periods_passed % 2 != 0:
if start_period == "AM":
new_period = "PM"
else:
new_period = "AM"
else:
new_period = start_period
# calculate days passed
if start_period == "PM" and periods_passed % 2 != 0:
days_passed = (periods_passed // 2) + 1
else:
days_passed = periods_passed // 2
if days_passed == 0:
days_later = 0
elif days_passed == 1:
days_later = "next day"
else: days_later = f"{days_passed} days later"
# present answer (day = None)
if not day:
if days_later == 0:
print(f"{new_hour}:{new_minute} {new_period}")
else:
print(f"{new_hour}:{new_minute} {new_period} ({days_later})")
# calculate new day
days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
if day:
start_day = re.findall("[a-z]+", day, re.IGNORECASE)[0].lower().capitalize()
for weekday in days_of_week:
if weekday == start_day:
start_day_index = days_of_week.index(weekday)
new_day = days_of_week[(start_day_index + days_passed) % 7]
# present answer (day provided)
if days_later == 0:
print(f"{new_hour}:{new_minute} {new_period}, {new_day}")
else:
print(f"{new_hour}:{new_minute} {new_period}, {new_day} ({days_later})")
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Challenge Information:
Scientific Computing with Python Projects - Time Calculator
Blockquote