Time_calculator project

Hi guys! I am new to Python and am currently doing a time calculator project and when i did my run test, my code failed with 2 tests my code is below. Can someone assist me where i am missing it. here is the link to my code https://repl.it/@KayDeeSibbs/boilerplate-time-calculator#time_calculator.py

Your code so far

def add_time(start, duration, day_of_week=""):

start_1 = [s.split() for s in start.split(":")]
duration_1 = duration.split(":")
hours = int(start_1[0][0]) + int(duration_1[0])
minutes = int(start_1[1][0]) + int(duration_1[1])

if minutes > 60:
    minutes = minutes - 60
    hours = hours + 1
else:
    pass

# Converting minutes to hours and PM to AM and vice versa
day_format = start_1[1][1]
if day_format == "AM" and hours < 12:
    day_format = "AM"
elif day_format == "AM" and hours >= 12:
    day_format = "PM"
elif day_format == "PM" and hours < 12:
    day_format = "PM"
elif day_format == "PM" and hours >= 12:
    day_format = "AM"

# Adding a leading zero to minutes less than 10
if minutes < 10:
    minutes = str(minutes).zfill(2)

# Converting hours to days
days = 0
days_later = 0
if hours > 12:
    number_of_days = hours / 24

    if number_of_days >= 1:
        days = round(number_of_days)

    elif number_of_days < 1:
        if day_format == "AM":
            days = 1

    hours = hours % 12
    days_later = days

    if hours % 12 == 0:
        hours = 12

elif hours < 12:
    days = 0
    days_later = days

# Determining the day of the week
days_of_week = [
    "monday", "tuesday", "wednesday", "thursday", "friday", "saturday",
    "sunday"
]

for day in days_of_week:
    day_of_week = day_of_week.lower()

    if day_of_week == day:
        day_of_week = day
        index_of_day = days_of_week.index(day_of_week)

        if days_later <= 7:
            new_day = days_later + index_of_day

        if days_later > 7:
            another_day = days_later - 7
            new_day = another_day + index_of_day

        if new_day > 6:
            new_day = ((new_day + 1) % 7) - 1

        new_day_of_week = days_of_week[new_day].capitalize()

if day_of_week == "":
    if days_later == 0:
        new_time = f"{hours}:{minutes} {day_format}"

    elif days_later == 1:
        new_time = f"{hours}:{minutes} {day_format} (next day)"

    else:
        new_time = f"{hours}:{minutes} {day_format} ({days_later} days later)"

if day_of_week != "":
    if days_later == 0:
        new_time = f"{hours}:{minutes} {day_format}, {new_day_of_week}"

    elif days_later < 2:
        new_time = f"{hours}:{minutes} {day_format}, {new_day_of_week} (next day)"

    else:
        new_time = f"{hours}:{minutes} {day_format}, {new_day_of_week} ({days_later} 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/87.0.4280.88 Safari/537.36.

Challenge: Time Calculator

Link to the challenge:

Welcome to the forums.

I ran your tests (always better to post the output) and you have the same problem twice:

    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "2:59 AM", "24:00" to return "2:59 AM"')
AssertionError: '2:59 PM (next day)' != '2:59 AM (next day)'
- 2:59 PM (next day)
?      ^
+ 2:59 AM (next day)
?      ^
 : Expected calling "add_time()" with "2:59 AM", "24:00" to return "2:59 AM"

which shows that the AM/PM is not correct. The code at fault must be then

    hours = int(start_1[0][0]) + int(duration_1[0])
    minutes = int(start_1[1][0]) + int(duration_1[1])

    if minutes > 60:
        minutes = minutes - 60
        hours = hours + 1
    else:
        pass

    # Converting minutes to hours and PM to AM and vice versa
    day_format = start_1[1][1]
    if day_format == "AM" and hours < 12:
        day_format = "AM"
    elif day_format == "AM" and hours >= 12:
        day_format = "PM"
    elif day_format == "PM" and hours < 12:
        day_format = "PM"
    elif day_format == "PM" and hours >= 12:
        day_format = "AM"

If you work through this code with the inputs 2:59 AM and 24:00, on addition you would have 26:59 AM, which would match your second condition, leaving 26:59 PM. Then later on, your code works out that a day has elapsed, and leaves you with 2:59 PM and the day, hence the test error.

Thanx let me try to work around it and see if i can win

Thanx a lot jeremy.a.gray. I worked around the problem and i solved it.

# Converting minutes to hours and PM to AM and vice versa
  day_format = start_1[1][1]
  new_hours = hours % 24 
  if day_format == "AM" and new_hours < 12:
      day_format = "AM"
  elif day_format == "AM" and new_hours >= 12:
      day_format = "PM"
  elif day_format == "PM" and new_hours < 12:
      day_format = "PM"
  elif day_format == "PM" and new_hours >= 12:
      day_format = "AM"
 python main.py
1:08 AM (next day)
............
----------------------------------------------------------------------
Ran 12 tests in 0.001s

OK
 

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