Make python script work every day at certain hours

I am trying to make a python script (infinite loop) work every day from 9am to 11pm, and this is happening again and again. I did some research and came up with this code at the end:

while True:
    if dt.now().hour in range(9, 23):  
        if __name__ == "__main__":

            """ Not important """

            while True:
                try:
                    """ bet_automation contains all the necessary code """

                    bet_automation()

                except Exception as ex:

                    """ Catch all the error of bet_automation() and restart program """

                    print_error(ex)
                    time.sleep(5)
                    if not isinstance(ex, LoginTimeoutException):
                        try:
                            driver = get_driver()
                            filename = str(time.time())
                            driver.get_screenshot_as_file("errors/{}.png".format(filename))
                            with io.open("errors/{}.html".format(filename)) as f:
                                f.write(unicode(driver.page_source))
                        except:
                            pass
                    try:
                        quit_driver()
                    except:
                        pass

    else:
        sys.exit(0)

Thus, the script manages to start at 20.00 and work correctly. Even if I start it earlier, it starts working only at 20.00, which is excellent, but it does not end at 21, which confuses.

I know well that this is probably a very simple and dumb question, but, as I said, I'm a beginner. I had this script programmed by a "professional" programmer, and I'm trying to edit and improve it, and I would like to do it myself to understand the whole process.

Every understanding is much appreciated,

Many thanks,

:)

+4
1

. , . ; - 9:00 23:00, dt.now().hour in range(9, 23) True, () . False, . , .

. , , . bet_automation() , . bet_automation() , , .

, - , , :

while True:
    try:
         bet_automation()
         if dt.now().hour not in range(9, 23):
             break
(...)
0

All Articles