Tell us what’s happening:
It passed the test but there is certainly something wrong. I could not figure out how to correct it. I have already planned to redo the whole thing later after I have some rest. Before I completely abandon this version, I want to see if someone else can help.
print(add_time("11:06 PM", "12:00", "Tuesday"))
11:06 AM, Wednesday (next day)
print(add_time("11:06 PM", "13:00", "Tuesday"))
12:06 AM, Thursday (2 days later)
the mistake would carry on until
print(add_time("11:06 PM", "23:00", "Tuesday"))
10:06 AM, Thursday (2 days later)
print(add_time("11:06 PM", "24:00", "Tuesday"))
11:06 PM, Wednesday (next day)
https://replit.com/@StephenChan1/freeCodeCampSCPtime-calculator#time_calculator.py
Your code so far
def add_time(start, duration, start_day=""):
# chopping up input for calculations
a = start.split()
start_time, AMPM = a[0], a[1]
b = start_time.split(":")
start_hr, start_min = int(b[0]), int(b[1])
c = duration.split(":")
dura_hr, dura_min = int(c[0]), int(c[1])
# setting up variable for return
new_min = 0
co_hr = 0 # for minute carry over to hour
new_hr = 0
new_day = 0
new_AMPM = ""
new_weekday = ""
days_later = ""
# minutes, carry over to hours at 60
minutes = start_min + dura_min
if minutes <= 59:
new_min = minutes
else:
new_min = minutes % 60
co_hr += 1
# hours, carry over to days
hours = start_hr + dura_hr + co_hr
if hours <= 11:
new_hr = hours
new_AMPM = AMPM
elif hours % 12 == 0:
new_hr = 12 # return 12:xx instead of 0
new_day += (hours // 24)
if AMPM == "AM":
new_AMPM = "PM"
else:
new_AMPM = "AM"
new_day += 1
else:
new_hr = hours % 12
new_day += hours // 24
if dura_hr % 24 == 0:
new_AMPM = AMPM
else:
if AMPM == "AM":
new_AMPM = "PM"
else:
new_AMPM = "AM"
new_day += 1
# n days later
if new_day == 1:
days_later = " (next day)"
elif new_day >= 2:
days_later = f" ({new_day} days later)"
# weekday
weekday = [", Monday", ", Tuesday", ", Wednesday",
", Thursday", ", Friday", ", Saturday", ", Sunday"]
if start_day:
weekday_lower = list(map(lambda x: x.lower().strip(", "), weekday))
start_day_idx = weekday_lower.index(start_day.lower())
if new_day == 0:
new_weekday = weekday[start_day_idx]
else:
new_day_idx = (start_day_idx + new_day) % 7
new_weekday = weekday[new_day_idx]
new_min = str(new_min).zfill(2) # zero padding minutes
new_time = f"{new_hr}:{new_min} {new_AMPM}{new_weekday}{days_later}"
return new_time
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Time Calculator
Link to the challenge: