Time Calculator error occurring in day calculation

I’m running into a point in my code where I can’t figure out what path the code is taking to reach the output. I think either my code is pulling from the wrong conditional to get the output, but Replit doesn’t generate any indication of the path python takes when reading the code.

I’m trying to tell the code “Here is the input day. If it’s in the “week” list, I need you to put a day in the output.” Instead, it’s just ignoring that instruction entirely.

I have tried going as far writing “If day == saturDay”, add “Sunday” to the code. It ignores this and still produces the same error output. How can I ensure the “day” parameter is never ignored?

Error:

Traceback (most recent call last):
  File "/home/runner/boilerplate-time-calculator/test_module.py", line 55, in test_twenty_four_with_day
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "2:59 AM", "24:00", "saturDay" to return "2:59 AM, Sunday (next day)"')
AssertionError: '2:59 AM (next day)' != '2:59 AM, Sunday (next day)'
- 2:59 AM (next day)
+ 2:59 AM, Sunday (next day)
?        ++++++++
 : Expected calling "add_time()" with "2:59 AM", "24:00", "saturDay" to return "2:59 AM, Sunday (next day)"

Code:

def add_time(start, duration, day = ''):

  sep = [str.split(":") for str in start.split(" ")]
  [start_hours, start_mins, period] = [*sep[0], *sep[1]]
  [added_hours, added_mins] = duration.split(':')

  days = 0
  hours = int(start_hours) + int(added_hours)
  minutes = int(start_mins) + int(added_mins)

  #remove AM/PM to do integer-based math
  hours += 12 if period == 'PM' else 0

  #convert to new hour:minute setup
  hours += minutes // 60
  minutes = minutes % 60
  #account for days passed in 24 hour increments
  days = hours // 24
  hours = hours % 24

  #convert back to AM-PM setup in 12 hour increments
  period = 'PM' if hours > 11 else 'AM'
  hours -= 12 if hours > 12 else 0
  hours = 12 if hours == 0 else hours

  #account for missing tens place in minutes
  if minutes < 10:
    minutes = str("0" + str(minutes))

  #create list of days to add to the new time
  week = ["Sunday", "Monday", "Tuesday", "Wednesday","Thursday","Friday","Saturday"]

  """
  if day in week and days > 0:
    day_index = week.index(day.capitalize())
    i = days % 7 if days > 7 else days
    new_day = week[day_index + i]"""
  
#Adjust for mispelled days
  day = day.capitalize()
#Check if time requires new day of the week
  if day in week and days < 1:
    new_day = day
  if day in week and days >= 1:
    day_index = week.index(day)
    new_loc = (day_index + days)%7
    new_day = week[new_loc]

  new_time = str(str(hours) + ":" + str(minutes) + " " + period)

  if days == 0:
    new_time
  elif days == 1:
    new_time += " (next day)"
  elif day in week:
    new_time += ", "+ new_day + " ("+str(days)+" days later)"
  elif day == '':
    new_time += " (" +str(days)+" 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/99.0.4844.74 Safari/537.36

Challenge: Time Calculator

Link to the challenge:

This code checks for one day of time passage

but does not check if a day was specified initially. It’s only (next day) with no day specified, but the other message if a day is specified.

1 Like