Better Way to Handle ''12 o clock'' case?

Hi All -

My code is here.

All tests pass but i expect the code is a bit clunky. Is there a better way to handle the 12 O’Clock case?


new_hour = new_hour%24
  
  if (new_hour >= 12):
    new_hour -= 12
    period = "PM"
  else:
    period = "AM"

  if (new_hour == 0):
    new_hour = 12

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15.

Challenge: Time Calculator

Link to the challenge:

Chain it with an elif maybe?
Is the only idea I would have.

period = "PM"
  if (new_hour > 12):
    new_hour -= 12
  elif new_hour < 12:
    period = "AM"

I got an error message.
"Name Error: name’new_hour’ is not defined.

Please try this :

hour = int(input('Enter the hour :'))
if hour >=12:
    print('The period is : PM')
else:
    print('The period is  :AM')

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