Scientific Computing with Python Projects - Time Calculator

Tell us what’s happening:
everything works but I’m receiving an error.

Your code so far

(https://replit.com/@J1Universe/boilerplate-time-calculator-1#time_calculator.py)
def add_time(start, duration, day=False):
  minute,period = start.split(" ")
  hour, minute = minute.split(":")
  endHour, endMinute = duration.split(":")
  hour, minute = int(hour), int(minute)
  endHour, endMinute = int(endHour), int(endMinute)
  index=0
  testday= ''
  dow=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
  hour=int(hour)
  minute=int(minute)
  endHour=int(endHour)
  endMinute=int(endMinute)
  testMinute=minute+endMinute
  testHour= hour+endHour
  period=period.lower()
  testMinute=minute+endMinute
  while testMinute >60:
    testMinute=testMinute-60
    testHour=testHour+1
  def days_later(index):
    if index==0:
      string=""
    elif index==1:
      string=" (next day)"
    elif index>=2:
      string= " ("+str(index) +" days later)"
    return string
  while testHour>=12:
    if testHour>12:
      if period=="am":
        testHour=testHour-12
        period="pm"
      elif period=="pm":
        index=index+1
        days_later(index)
        testHour=testHour-12
        period="am"
    elif testHour==12:
      if period=="am":
        period="pm"
      elif period=="pm":
        index=index+1
        days_later(index)
        period="am"
      break
  if day:
    for i in range(6):
      if day.lower()==dow[i].lower():
        testday=', '+dow[i+index]
  period=period.upper()
  return(f'{testHour}:{testMinute:02} '+period+testday +days_later(index))


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

Challenge: Scientific Computing with Python Projects - Time Calculator

Link to the challenge:

Could you tell us what error you see?

1:08 AM (next day)
..E.........
======================================================================
ERROR: test_high_duration_with_day (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-time-calculator-1/test_module.py", line 63, in test_high_duration_with_day
    actual = add_time("8:16 PM", "466:02", "tuesday")
  File "/home/runner/boilerplate-time-calculator-1/time_calculator.py", line 50, in add_time
    testday=', '+dow[i+index]
IndexError: list index out of range

----------------------------------------------------------------------
Ran 12 tests in 0.002s

FAILED (errors=1)
 

I’m not sure if you solved it or not yet but the error is saying that the ‘dow[i+index]’ index number doesn’t exist, it is “out of the range” of index numbers. This is because your dow list only has 7 items in the list and its index range is only the numbers 0-6. This works for problems that move forward in time by a week but anything that exceeds that time, will be out of range.

For example, the problem being tested was,

actual = add_time(“8:16 PM”, “466:02”, “tuesday”)

This would be 19 days later.

It fails because your list is too small. The issue is, you can’t build an infinite list of weekdays because someone could keep inputting a higher number of duration hours.
hint: You’ll need to use a while loop for this part, bc while loops run infinitely until they are otherwise told (solution was a lot simpler than I expected using the while loop)

Also, I’m assuming it works when you take away the weekday parameter, and you probably get the correct output which ends in ‘(19 days later)’.
That is just because the part of your code where you are finding ‘n days later’ and the 'day of the week ’ are completely separate.

The code needs to be written to solve for ANY possible input of duration hours, this means that someone should be able to add thousands of hours and it will still work the same way (using the while loop mentioned above).

I’m sure they don’t have to be related to each other for every possible solution but in my code…
I made a variable called ‘days’ that first tracked the number of days in one part of my code and then I later used that ‘days’ variable, within the while loop, to “normalize” the number of days to fit into the ‘dow’ list, no matter how many days passed. If you can figure that part out then it should pass. If you did all of that, then you could probably delete the ‘def days_later(index)’ function as well and when you return your results at the end, just input the ‘days’ variable like this for example…
(f’{testHour}:{testMinute:02} '+period+testday + ({days} days later)).

I hope this helps at all and if this totally missed the mark then please let me know. I’m pretty new to this stuff so I’m learning as well!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.