Home > Software engineering >  How can i put some condition in While True loop
How can i put some condition in While True loop

Time:03-15

I'm using Schedule module in python, for scheduling my code so for that i used While True -

And here i have four Schedule time.

schedule.every().day.at(schedule_time).do(run_code)
while True:
   schedule.run_pending()
   time.sleep(2)

and i'm running this code with the help of Telegram. with the help of Telepot module

def handle(msg):
    text_data = msg['text']
    if (text_data == 'Start):
        message = initial_message()
        bot.sendMessage(1753352834, "Schedule Code starts running")
        # Schedule_code function is calling for run my entire main code
        schedule_code()
    else:
        bot.sendMessage(1753352834, "Sorry, I don't understand your mean, you should write Start")
MessageLoop(bot, handle).run_as_thread()

When i writes on telegram start so this code starts run, but i want to stop this code with the help of Telegram but i don't understand how.

I don't know how can i stop this While true function- I want, When i write Stop on telegram so my entire code would stop.

if (text_data == 'Stop):
   sys.exit()
else:
    pass

CodePudding user response:

If you want to quit your program you could use

while True:
    quit() #Stops the python script

If you only want to go outside of the loop you could use

while True:
    break

But the best way to do it is with a condition. You could use something like

stop = False
while not stop:
    #Do something
    if text_data == "Stop":
        stop = True
  • Related