Reputation: 1181
In my program, I grab the current time. I then want to check if the current time is greater than t1 and less than t2. For example:
00,00 <= dt.time() <= 06,00
06,01 <= dt.time() <= 12,00
12,01 <= dt.time() <= 18,00
etc...
What I am doing is checking which quandrant of the day the current time is in.
If one out of the 4 statements ends up being true, then my program will move on to another function.
Right now, my program is acting like all 4 statements are true and it is looping several times.
When I print everything to see what it is doing, it looks like each of the 4 objects in t1 are only being compared to the last of the 4 objects in t2.
Is there a way to do this properly?
Here is what I am working on...
from datetime import datetime, date, time
p1 = 1
p2 = 2
p3 = 3
p4 = 4
what_number_should_be = 3
def cycle():
dt = datetime.now()
t1 = [time(00,00), time(06,01), time(12,01), time(18,01)]
t2 = [time(06,00), time(12,00), time(18,00), time(23,59)]
user_number = [p1, p2, p3, p4]
# I think the next 3 lines are throwing me off
for x in t1:
for y in t2:
for z in user_number:
# check if current date is between times listed in t1 and t2
if x <= dt.time() <= y:
print(x, dt.time(), y)
print(z)
# if above is true, then check
# if user_number does not equal 3
if what_number_should_be != z:
print(z, " did not equal ", what_number_should_be)
# should only end up here once
cycle()
Upvotes: 0
Views: 110
Reputation: 11
You can zip t1, t2, and user_number array together, which would result in a list of tuples of
[(time(00,00), time(06,00), p1), (time(06,01), time(12,00), p2), (time(12,01), time(18,00), p3), (time(18,01), time(23,59), p4)]
and then run the loop like this
for x in zip(t1, t2, user_number):
if x[0] <= dt.time() <= x[1]:
print(x[0], dt.time(), x[1])
print(x[2])
if what_number_should_be != x[2]:
print(x[2], " did not equal ", what_number_should_be)
Upvotes: 1
Reputation: 41
If I understand correctly, for a given time dt
, you want to determine the 'quadrant' of the day in which it resides, which you have chosen to encode in the following way:
dt
falls between 00,00 and 06,00, then it is in quadrant 1
dt
falls between 06,01 and 12,00, then it is in quadrant 2
dt
falls between 12,01 and 18,00, then it is in quadrant 3
dt
falls between 18,01 and 23,59, then it is in quadrant 4
I also gather that you expect your for-loop to print only 3
because you happen to be running this program between 12,01 and 18,00 in real time.
If my understanding of the problem is correct, then your use of two arrays to accomplish this is clever, and we can use them to discover the solution. By observing that because there are exactly 4 quadrants that we want to check for dt
's inclusion in, we can deduce that only need to iterate exactly 4 times, once for each of these quadrants.
This can be written as such:
dt = datetime.now()
t1 = [time(00,00), time(06,01), time(12,01), time(18,01)]
t2 = [time(06,00), time(12,00), time(18,00), time(23,59)]
for quadrant in range(4):
if (t1[quadrant] <= dt and dt <= t2[quadrant]):
print("The current time is in quadrant: {0}".format(quadrant))
I hope this helps somewhat!
Upvotes: 0
Reputation: 81
If I'm understanding the question correctly, you can replace your 3 for
loops with
for x, y, z in zip(t1, t2, user_number):
.
Zip gives you the corresponding elements from each list: zip Documentation
Upvotes: 1