Reputation: 53
CODE:
import datetime
data = [('09', '55', None, 'AC is on', None), ('10', '00',None, 'AC is on', None),('10', '13','fan is on', 'AC is on', 'light is on')]
def calc(data):
print(data) #prints the whole table
while True:
h=datetime.datetime.today().strftime("%H")
mi=datetime.datetime.today().strftime("%M")
# z=[(i[2],i[3],i[4]) for i in data if i[0] == h and i[1]==mi]
for i in data:
if i[0] == h and i[1]==mi:
print (i[2],i[3],i[4])
# sleep(60)
break
if __name__ == '__main__':
calc(data)
The first 2 elements inside 'data' are hour and minutes entered by user. The code should take user entered values , that is, data here. and should check with current time and should print i[2],i[3],i[4] as shown in code.
1)I just want the values to be printed once. But, it keeps on checking and prints value for 1 whole minute. The break statement isn't working.
2)Also, is it possible to somehow check and not to print the none?
Upvotes: 0
Views: 61
Reputation: 551
If I understood correctly you want your program to run until it finds a match between the current time, and the time specified in one of the tuples (i
) in data
: (data[i][0], data[i][1])
, and then print the rest of the tuple : i[2:5]
break
is in the for
loop scope, not your while
loop. You exit the for
loop, yes, not your infinite while True
loop.while match_not_found:
, then set match_not_found = True
, and have a line match_not_found = False
before your break
statement.if
to exclude None
of the printed itemsLike that :
import datetime
data = [('09', '55', None, 'AC is on', None), ('10', '00',None, 'AC is on', None),('10', '13','fan is on', 'AC is on', 'light is on')]
def calc(data):
print(data) #prints the whole table
match_not_found = True
while match_not_found:
h=datetime.datetime.today().strftime("%H")
mi=datetime.datetime.today().strftime("%M")
# z=[(i[2],i[3],i[4]) for i in data if i[0] == h and i[1]==mi]
for i in data:
if i[0] == h and i[1]==mi:
print ([j for j in i[2:5] if j != None])
match_not_found = False
break
if __name__ == '__main__':
calc(data)
Upvotes: 1
Reputation: 689
I have done few changes in code.
I dont know why you are using While= True . Because of which it is NOT coming out of loop . So i have removed it
Just added else statement to for check , you can remove it
import datetime
data = [('09', '55', None, 'AC is on', None), ('10', '00',None, 'AC is on', None),('10', '13','fan is on', 'AC is on', 'light is on')]
def calc(data):
print(data) #prints the whole table
# while True:
h=datetime.datetime.today().strftime("%H")
print(h)
mi=datetime.datetime.today().strftime("%M")
print(mi)
# z=[(i[2],i[3],i[4]) for i in data if i[0] == h and i[1]==mi]
for i in data:
if i[0] == h and i[1]==mi:
print (i[2],i[3],i[4])
else:
print ("It does NOT match")
# sleep(60)
break
if __name__ == '__main__':
calc(data)
EDITED (works now):
import datetime
data = [('09', '55', None, 'AC is on', None), ('10', '00',None, 'AC is on', None),('10', '52','fan is on', 'AC is on', None)]
def calc(data):
print(data) #prints the whole table
while True:
h=datetime.datetime.today().strftime("%H")
mi=datetime.datetime.today().strftime("%M")
# z=[(i[2],i[3],i[4]) for i in data if i[0] == h and i[1]==mi]
for i in data:
if i[0] == h and i[1]==mi:
print (i[2],i[3],i[4])
# sleep(60)
exit()
if __name__ == '__main__':
calc(data)
Upvotes: 0