Loop Function like to Arduino in Python -


i new @ programming , had knowledge of arduino c coding, however, wanted move on more complex coding raspberry pi in python. how in arduino can enter loop(), however, cannot find similar in python. coding program repeatedly checks time, , when time hits point, starts function in code. "while true:" loop not seem work. please disregard formatting in coding. when copied formatting screwed up. coding basic idea of main program. when hits time, need print something.

    while true:         import datetime         if datetime.time (12, 15, 17, 000000):            print "test"            break 

your while loop looks correct me. problem (i believe) don't quite understand how current time, , compare time interested in.

datetime.time doesn't default current time, structure contains time. time, 1:00:01 am, 1:13:00 pm (now), etc.

if want current time, use datetime.datetime.now().time(). does, ask datetime class "hey, current date , time?", , returns datetime object containing data. since interested in current time, adding .time() return time object contains current time. should replace datetime.time in code.

as comparing (the (12, 15, 17, 000000)), need tell python using time object using datetime.time(12, 15, 17, 000000) instead.

also, mentioned should use == when testing equality. is keyword tells if 2 variables point same object, not if 2 different objects equal. unless you're sure should use is, should use == instead.

overall, believe should work:

import datetime  while true:     if datetime.datetime.now().time() == datetime.time(12, 15, 17, 000000):        print "test"        break 

see this documentation on datetime library.

edit: since don't have enough reputation comment on question here's how program work specific date:

according documentation here, construct datetime object using datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond]]]]) each additional set of brackets being optional.

so things have change code above use datetime object instead of time object, means need rid of .time() part, , use datetime.datetime() constructor instead of datetime.time() constructor.

import datetime  while true:     my_date = datetime.datetime(year, month, day, hour, minute, second, microsecond)     if datetime.datetime.now() == my_date:        print "test"        break 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -