Need a bit of guidance for project 2 #TimeCalculator

been spending hours and hours to try and figure out why it is not working but with no success, appreciated if some guidance can be made what mistakes were make and what can be improved on as a beginner.

def add_time(start, duration, days=False):
  
    current_time = start.split(':')
    current_time_mins = current_time[1].split()
    current_hrs = int(current_time[0])
    current_mins = int(current_time_mins[0])
    current_meridiem = current_time_mins[1]
    meridiem_changes = {"AM": "PM", "PM": "AM"}

    added_time = duration.split(":")
    added_hrs = int(added_time[0])
    added_mins = int(added_time[1])

    the_days_in_the_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    the_days_in_the_week_with_index = {"monday": 0, "tuesday": 1, "wednesday": 2, "thursday": 3, "friday": 4, "saturday": 5, "sunday": 6 }

    num_of_days = int(added_hrs/24)
  
    new_mins = current_mins + added_mins

    if new_mins > 59 :
      current_hrs += 1
      new_mins %= 60
    new_hrs = (current_hrs + added_hrs) % 12
    num_of_meridiem_changes = int((current_hrs + added_hrs)/12)
  
    if new_mins >= 10 :
      new_mins = new_mins
    else:
      "0" + str(new_mins)

    if new_hrs == 0 :
      new_hrs = 12
    else:
      new_hrs = new_hrs
  
    if current_meridiem == "PM" and current_hrs + (added_hrs%12) >= 12 :
      num_of_days += 1

    if num_of_meridiem_changes % 2 == 1 :
      current_meridiem = meridiem_changes[current_meridiem]
    else:
      current_meridiem = current_meridiem

    new_time = str(new_hrs) + ":" + str(new_mins) + " " + current_meridiem
  
    if (days) :
      days = days.lower()
      index = int((the_days_in_the_week_with_index[days]) + num_of_days) % 7
      new_days = the_days_in_the_week[index]
      new_time = ", " + new_days

    if num_of_days == 1 :
      return new_time + " " + "(next day)"
    elif num_of_days > 1 :
      return new_time + " (" + str(num_of_days) + " days later)"

    return new_time

Please provide a link to your replit - as it has some implemented test-cases which allow us to actually figure out where issues might come from.

https://replit.com/@DarkMiku1/boilerplate-time-calculator-1#time_calculator.py
this is the link to the repl

It seems like half the errors are '5:1 AM' != '5:01 AM' → which just means you have to make sure minutes are always displayed as two digits.

The other half is ', Monday' != '5:42 PM, Monday' → meaning for whatever reason if day-names are given, you don’t return a time.

Overall it looks like your calculations are correct, so I suspect resolving the errors shouldn’t be much of a challenge :wink:

thank you for the guidance, i solved it within minutes after taking the advice. Really appreciated :smile:

1 Like

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