Freecodecamp Ti

Tell us what’s happening:
Could someone help me !

  • I’m facing problems with some AM and PM
  • For the next day
  • and for one final day

for some tests the code is working well but not for all the tests
And thank you in advance for helping me !

Your code so far

def add_time(start_time, duration, start_day=False):

  week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]

  new_day = ""
  hours_add = 0
  hours = 0
  

  start = start_time.split(" ")[0]
  hours_st = start.split(":")[0]
  minutes_st = start.split(":")[1]
  form = start_time.split(" ")[1]

  dur = duration
  hours_dur = dur.split(":")[0]
  minutes_dur = dur.split(":")[1]

#calculating sum of hours and sum of minutes
  hours = int(hours_st) + int(hours_dur)
  minut = int(minutes_st) + int(minutes_dur)

#calculating hours to add
  if minut >= 60:
    hours_add = (minut // 60)
    minut = minut % 60
  if minut <= 9:
    minut = f"0{minut}"
  hours += hours_add
  days_add = hours // 24
  
 
#calculating days to add
  if hours > 24:
    days_add = hours / 24
    if 0 < days_add - (hours//24) < 0.5:
      days_add = hours // 24
    elif days_add - (hours//24) > 0.5:
      days_add = (hours // 24) + 1
    else:
      days_add = hours // 24
    
    
  
  days_later = ""
  hours = hours % 12

  if hours == 0:
    hours = 12
  else:
    hours = hours

  form_nbflips = hours / 12
  form_flip = {"AM":"PM", "PM":"AM"}

  if form == "AM" and (int(hours_st) + int(hours_dur)) > 12:
    form = "PM"
  elif form == "PM" and (int(hours_st) + int(hours_dur)) > 12:
    form = "AM"
  if form_nbflips % 2 == 1:
    form = form_flip[form]
     
  else:
    form = form

  if form == "PM" and hours + (hours_add % 12) >= 12:
    days_add += 1
    
    
  if days_add == 1:
    days_later = " (next day)"
  if days_add > 1:
    days_later = f" ({days_add} days later)"
    

  if start_day:
    d = str(start_day)
    d = d.lower()
    idx = week.index(d)

    if days_add == 7:
      new_day = d.capitalize()
    if days_add > 7:
      nday = (days_add % 7)
      nday += idx
      if nday >= 7:
        nday = (days_add % 7)
        
      new_day = week[nday].capitalize()
    else:
      nday = days_add + idx
      new_day = week[nday].capitalize()
    
    new_day = f" {new_day}"

  if start_day:
    final = f"{hours}:{minut} {form},{new_day}{days_later}"

    
  else:
      final = f"{hours}:{minut} {form}{days_later}"


  return final

Your browser information:

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

Challenge: Time Calculator

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Ok, Thank you for the advice.

Which cases are working, which aren’t? Have you tried narrowing down, which part of code is resulting in wrong result?

Sorry for the delay of the response!
Please find below the test module and then you will find the Failure that I Had
Test module:
lass UnitTests(unittest.TestCase):

def test_same_period(self):
    actual = add_time("3:30 PM", "2:12")
    expected = "5:42 PM"
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "3:30 PM", "2:12" to return "5:42 PM"')

def test_different_period(self):
    actual = add_time("11:55 AM", "3:12")
    expected = "3:07 PM"
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "11:55 AM", "3:12" to return "3:07 PM"')

def test_next_day(self):
    actual = add_time("9:15 PM", "5:30")
    expected = "2:45 AM (next day)"
    self.assertEqual(actual, expected, 'Expected time to end with "(next day)" when it is the next day.')

def test_period_change_at_twelve(self):
    actual = add_time("11:40 AM", "0:25")
    expected = "12:05 PM"
    self.assertEqual(actual, expected, 'Expected period to change from AM to PM at 12:00')

def test_twenty_four(self):
    actual = add_time("2:59 AM", "24:00")
    expected = "2:59 AM (next day)"
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "2:59 AM", "24:00" to return "2:59 AM"')

def test_two_days_later(self):
    actual = add_time("11:59 PM", "24:05")
    expected = "12:04 AM (2 days later)"
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "11:59 PM", "24:05" to return "12:04 AM (2 days later)"')

def test_high_duration(self):
    actual = add_time("8:16 PM", "466:02")
    expected = "6:18 AM (20 days later)"
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "8:16 PM", "466:02" to return "6:18 AM (20 days later)"')

def test_no_change(self):
    actual = add_time("5:01 AM", "0:00")
    expected = "5:01 AM"
    self.assertEqual(actual, expected, 'Expected adding 0:00 to return initial time.')

def test_same_period_with_day(self):
    actual = add_time("3:30 PM", "2:12", "Monday")
    expected = "5:42 PM, Monday"
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "3:30 PM", "2:12", "Monday" to return "5:42 PM, Monday"')

def test_twenty_four_with_day(self):
    actual = add_time("2:59 AM", "24:00", "saturDay")
    expected = "2:59 AM, Sunday (next day)"
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "2:59 AM", "24:00", "saturDay" to return "2:59 AM, Sunday (next day)"')

def test_two_days_later_with_day(self):
    actual = add_time("11:59 PM", "24:05", "Wednesday")
    expected = "12:04 AM, Friday (2 days later)"
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "11:59 PM", "24:05", "Wednesday" to return "12:04 AM, Friday (2 days later)"')

def test_high_duration_with_day(self):
    actual = add_time("8:16 PM", "466:02", "tuesday")
    expected = "6:18 AM, Monday (20 days later)"
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "8:16 PM", "466:02", "tuesday" to return "6:18 AM, Monday (20 days later)"')

if name == “main”:
unittest.main()

Fails:
python main.py
…FF.F…FFFF

FAIL: test_high_duration_with_day (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-time-calculator/test_module.py”, line 65, in test_high_duration_with_day
self.assertEqual(actual, expected, ‘Expected calling “add_time()” with “8:16 PM”, “466:02”, “tuesday” to return “6:18 AM, Monday (20 days later)”’)
AssertionError: ‘6:18 AM, Sunday (20 days later)’ != ‘6:18 AM, Monday (20 days later)’

  • 6:18 AM, Sunday (20 days later)
    ? ^^
  • 6:18 AM, Monday (20 days later)
    ? ^^
    : Expected calling “add_time()” with “8:16 PM”, “466:02”, “tuesday” to return “6:18 AM, Monday (20 days later)”

======================================================================
FAIL: test_next_day (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-time-calculator/test_module.py”, line 20, in test_next_day
self.assertEqual(actual, expected, ‘Expected time to end with “(next day)” when it is the next day.’)
AssertionError: ‘2:45 AM’ != ‘2:45 AM (next day)’

  • 2:45 AM
  • 2:45 AM (next day)
    : Expected time to end with “(next day)” when it is the next day.

======================================================================
FAIL: test_period_change_at_twelve (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-time-calculator/test_module.py”, line 25, in test_period_change_at_twelve
self.assertEqual(actual, expected, ‘Expected period to change from AM to PM at 12:00’)
AssertionError: ‘12:05 PM (next day)’ != ‘12:05 PM’

  • 12:05 PM (next day)
  • 12:05 PM
    : Expected period to change from AM to PM at 12:00

======================================================================
FAIL: test_twenty_four (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-time-calculator/test_module.py”, line 30, in test_twenty_four
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”

======================================================================
FAIL: test_twenty_four_with_day (test_module.UnitTests)

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 PM, Sunday (next day)’ != ‘2:59 AM, Sunday (next day)’

  • 2:59 PM, Sunday (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)”

======================================================================
FAIL: test_two_days_later (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-time-calculator/test_module.py”, line 35, in test_two_days_later
self.assertEqual(actual, expected, ‘Expected calling “add_time()” with “11:59 PM”, “24:05” to return “12:04 AM (2 days later)”’)
AssertionError: ‘12:04 PM (2 days later)’ != ‘12:04 AM (2 days later)’

  • 12:04 PM (2 days later)
    ? ^
  • 12:04 AM (2 days later)
    ? ^
    : Expected calling “add_time()” with “11:59 PM”, “24:05” to return “12:04 AM (2 days later)”

======================================================================
FAIL: test_two_days_later_with_day (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-time-calculator/test_module.py”, line 60, in test_two_days_later_with_day
self.assertEqual(actual, expected, ‘Expected calling “add_time()” with “11:59 PM”, “24:05”, “Wednesday” to return “12:04 AM, Friday (2 days later)”’)
AssertionError: ‘12:04 PM, Friday (2 days later)’ != ‘12:04 AM, Friday (2 days later)’

  • 12:04 PM, Friday (2 days later)
    ? ^
  • 12:04 AM, Friday (2 days later)
    ? ^
    : Expected calling “add_time()” with “11:59 PM”, “24:05”, “Wednesday” to return “12:04 AM, Friday (2 days later)”

Ran 12 tests in 0.006s

FAILED (failures=7)