Time Calculator hint

Tell us what’s happening:

that’s my output:
5:42 PM
3:07 PM
2:45 AM (next day)
12:05 AM
2:59 PM (next day)
12:04 AM (2 days later)
6:18 AM (20 days later)
5:01 AM
5:42 PM, Monday
2:59 PM, Sunday (next day)
12:04 AM, Friday (2 days later)
6:18 AM, Monday (20 days later)

and that is the expected output:
“5:42 PM”
“3:07 PM”
“2:45 AM (next day)”
“12:05 PM”
“2:59 AM (next day)”
“12:04 AM (2 days later)”
“6:18 AM (20 days later)”
“5:01 AM”
“5:42 PM, Monday”
“2:59 AM, Sunday (next day)”
“12:04 AM, Friday (2 days later)”
“6:18 AM, Monday (20 days later)”

can someone give me a hint to figure out why sometimes AM don’t change to PM and viceversa?

Your code so far

def add_time(start, duration, day = None):
    starting_hour, form = start.split()
    start_hrs, start_min = starting_hour.split(':')

    dur_hrs, dur_min = duration.split(':')

    total_hrs = int(start_hrs) + int(dur_hrs)
    total_min = int(start_min) + int(dur_min)
    day_ = 0
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

    day_ = total_hrs / 24
    if day_ < 1:
        day_ = 0
    if day_ > 1:
        day_ = int(day_)

    if total_min > 59:
        total_hrs += 1
        total_min -= 60
    if total_hrs > 12:
        total_hrs %= 12
        if total_hrs == 0:
            total_hrs = 12
            
        if form == 'AM':
            form = 'PM'
        else:
            form = 'AM'
            day_ += 1


    time = f"{total_hrs}:{str(total_min).zfill(2)} {form}"

    if day:
        current_day_index = days.index(day.title())
        next_day_index = int((current_day_index + day_) % 7)
        time += f", {days[next_day_index]}"
    if day_ == 1:
        time += " (next day)"
    if day_ > 1:
        time += f" ({int(day_)} days later)"
    return 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:

If I run your code through the tests, I get the following errors:

FAIL: test_period_change_at_twelve (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/gray/src/work/fcc-sc-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 AM' != '12:05 PM'
- 12:05 AM
?       ^
+ 12:05 PM
?       ^
 : Expected period to change from AM to PM at 12:00

This is indicating that you are not including 12 at your AM/PM transition here

The next two errors have the same cause:

FAIL: test_twenty_four (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/gray/src/work/fcc-sc-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/gray/src/work/fcc-sc-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)"

There are more than 12 hours of change, which is triggering your AM/PM code, but it’s also 24 hours of change, which means it should have the same AM/PM designation. The cause is here

where the number of days of time change is calculated, but the total_hrs is not decreased by a multiple of 24.

thank you!

changed my code to

    if total_min > 59:
        total_hrs += 1
        total_min %= 60
    while total_hrs > 11:
        total_hrs -= 12
        if form == "AM":
            form = 'PM'
            day_ -=1
        else:
            form = "AM"
            day_ += 1

    if total_hrs == 0:
        total_hrs = 12

now it works!