-1

I am trying to see if the current hour, time and section is before the due hour, due minute and due section then it should print true otherwise false. My code is not working and ive been working on this for 2 hours

current_hour = 12
current_minute = 37
current_section = "PM"
due_hour = 9
due_minute = 0
due_section = "AM"



if  (((current_hour < 9) and (current_hour != 12))  and (current_minute != 0) and current_section):

    print("True")

else:

    print("False")
Riza
  • 25
  • 6
  • you need a solution that uses only the 6 variables you provided? – Nicolas Gervais Jan 20 '20 at 01:04
  • It isn’t clear to me what the issue/question is. As an aside, using strings for what should be a boolean value is a bad idea. In fact, instead of an if statement, you can simply use the expression itself. – AMC Jan 20 '20 at 02:53

1 Answers1

1

Your current code is failing (presumably) because you're using 'and current_section' which will pass True for any value of current_selection.

Using the datetime library makes this quite simple:

from datetime import datetime
due_time = datetime.strptime('9:00AM','%I:%M%p')
curr_time = datetime.strptime('12:37PM','%I:%M%p')
diff_seconds = (curr_time - due_time).total_seconds()
if diff_seconds > 0:
     print('False')
else:
     print('True')

You can also add dates to make it more robust (see https://stackoverflow.com/a/466376/10475762 for more information on how to use strptime).

jhso
  • 3,103
  • 1
  • 5
  • 13
  • Thanks! Im going to read more about the datetime library and play with it to understand how it works – Riza Jan 20 '20 at 04:10