Time Calculator - calculating days and AM/PM

I’m having trouble calculating the number of days after, and changing the time to AM or PM accordingly:

F....F..FFFF
======================================================================
FAIL: test_different_period (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/boilerplate-time-calculator/test_module.py", line 15, in test_different_period
    self.assertEqual(actual, expected, 'Expected calling "add_time()" with "11:55 AM", "3:12" to return "3:07 PM"')
AssertionError: '3:07 PM (next day)' != '3:07 PM'
- 3:07 PM (next day)
+ 3:07 PM
 : Expected calling "add_time()" with "11:55 AM", "3:12" to return "3:07 PM"

======================================================================
FAIL: test_period_change_at_twelve (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/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 AM' != '12:05 PM'
- 12:05 AM
?       ^
+ 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 "/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 AM (2 days later)' != '2:59 AM (next day)'
- 2:59 AM (2 days later)
+ 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 "/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 AM, Monday (2 days later)' != '2:59 AM, Sunday (next day)'
- 2:59 AM, Monday (2 days later)
+ 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 "/boilerplate-time-calculator/test_module.pyy", 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 "/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.002s

FAILED (failures=6)
3:02 PM (next day)

This bit of code is certainly giving me these errors:

 hr_res=s_hr+d_hr #Adding the hours of the start time and duration time
    mi_res=s_mi+d_mi #Adding the minutes of the start time and duration time
    if mi_res>59:
        hr_res+=1
        mi_res-=60
    while hr_res>12:
        if h12=='AM':
            h12=h12.replace('AM','PM')
        elif h12=='PM':
            h12=h12.replace('PM','AM')
        hr_res-=12
        days+=0.5
    days=round(days+0.5) #Trying to round the days into whole number

This part of your code gives some clues, so please see if this can help you:

days=(round(days=0.5) adds another 0.5 days to the total from the while loop. This causes 1 day later to become 2 days later (1 day from the wile loop, add 0.5 from the days+ 0.5 part = 1.5 and round this to 2 days with round().
Same goes for a new time on the same day: this would be 0 days, but 0.5 days is added and rounded to 1 day.
Is there a reason behind adding the 0.5 day in round(days+0.5)?

The loop to determine wether it’s AM or PM calculates with hr_res>12. In the last problem (11:59 PM + 24:05) your hr_res = 36, leaving exactly 12 before the final iteration, so it skips the while loop and does NOT change AM to PM anymore.
Solving this should not be too hard, you’ll end up with 0:04 AM, which gives you a whole new issue (the outcome should be 12:XX, not 0:XX…but maybe you’ve covered that in another part of your code already?)

Can you start with this and let us know if you were able to solve everything (or come back and post a link to the complete code including the adjustments based on the comment above, so we can look into it again with you)? :slight_smile:

1 Like

I feel so stupid. I forgot that >12 does not include 12!! Thanks for the pointer! :smile:

When I was testing this previously, some of the problems would show it as the same day, and not the next. Because the number of days would be 0.5, which is just 0 when rounded on Python. So I added 0.5 at the end of the loop each time to try to sort that out. But clearly this led to other problems.

So after adjusting the code, I got this (the same kind of problem as said above):

...F........
======================================================================
FAIL: test_next_day (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/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.

----------------------------------------------------------------------
Ran 12 tests in 0.001s

FAILED (failures=1)
12:04 AM (2 days later)

This is the adjusted code:

hr_res=s_hr+d_hr #Adding the hours of the start time and duration time
    mi_res=s_mi+d_mi #Adding the minutes of the start time and duration time
    if mi_res>59:
        hr_res+=1
        mi_res-=60
    while hr_res>11:
        if h12=='AM':
            h12=h12.replace('AM','PM')
        elif h12=='PM':
            h12=h12.replace('PM','AM')
        hr_res-=12
        days+=0.5
    days=round(days) #Trying to round the days into whole number
    if hr_res==0:
        hr_res=12

I completely forgot about that :rofl:
Added in code above.

1 Like

Nice work, only 1 Fail left is not a bad score at all!
You probably have the solution for X days later somewhere in your code, so it should not be too hard making sure “next day” shows when it’s supposed to

1 Like

I’ve finally done it!

For this specific problem, I had to create another specific condition in the ‘X days later’ section and a new variable for that.

That new variable is just the original hr_res value, which I called h:

hr_res=h=s_hr+d_hr #Adding the hours of the start time and duration time

For this problem, (hr_res originally before undergoing the while loop of subtracting 12, hence new value) h is greater than 11 hours (14 hours). Only then would it be considered the next day, so if h is 10 for example, it would still be within the same day.

But of course, this only makes sense when transitioning specifically from PM to AM. AM to PM is still the same day.

So I added these conditions in the ‘X days later’ section, and specifically at the top of that:

    if days==0 and mi_res<10 and h12=='AM' and h>11:
        if dow==None:
            return str(hr_res)+':'+'0'+str(mi_res)+' '+h12+' (next day)'
        elif dow!=None:
            weekday=week[(week.index(dow.title())+1) %len(week)]
            return str(hr_res)+':'+'0'+str(mi_rest)+' '+h12+', '+weekday+' (next day)'
    elif days==0 and mi_res>9 and h12=='AM' and h>11:
        if dow==None:
            return str(hr_res)+':'+str(mi_res)+' '+h12+' (next day)'
        elif dow!=None:
            weekday=week[(week.index(dow.title())+1) %len(week)]
            return str(hr_res)+':'+str(mi_res)+' '+h12+', '+weekday+' (next day)'

My other conditions are if days==0 [same day] (without the h12 and h added), if days==1 [next day] and if days>1 [x days later].

I added the new if days==0 before the plain one, because if I put it after, it would get caught in the plain one (since it satisfies the conditions on paper) and thus skips the new if days==0 condition.

Thanks Brain150. You are indeed the brain!! :smile:

1 Like

You’re welcome :grinning:
You did it, I just played the role of annoying teacher :wink:

1 Like

Hi all,
I don’t have any particular question on this challenge but I’m totally new to coding and I have just been learning on my own mostly and doing challenge on FCC courses.

This forum provided great hints into completing those challenges. I was wondering is there anywhere I can ask for feedback on my code in terms of how to write cleaner, more efficient, etc…

Thank you all!