Python Forum

Full Version: While True loop help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
so I'm working on an alarm for a virtual assistant the alarm is running in a while true loop while it waits for the time to go off. However I would like to leave that while loop to preform other functions with the virtual assistant but keep it running in the back ground is this possible here is the code let me know.
import datetime
import os
import random
from gtts import gTTS
from playsound import playsound

def assistant_response(text):
    print(text)
    tts = gTTS(text=text, lang="en-US", slow=False, )
    filename = "voicee.mp3"
    tts.save(filename)
    playsound(filename)


while True:

    text = input("cmd: ")
    if "hi" in text:
        assistant_response("hi")

    elif "alarm" in text:
        alarm_hour = int(input("Set hour: "))
        alarm_minutes = int(input("Set minutes: "))
        am_pm = input("am or pm? ")
        print(f"Waiting for time: {alarm_hour}:{alarm_minutes} {am_pm}")

        if am_pm == 'pm':
            alarm_hour += 12
        elif alarm_hour == 12 and am_pm == 'am':
            alarm_hour -= 12
        else:
            pass

        while True:

            if alarm_hour == datetime.datetime.now().hour and alarm_minutes == datetime.datetime.now().minute:
                print("\nIt's the time!")
                assistant_response("alarm")
                break
you can create a new thread for the alarm to loop in
https://realpython.com/intro-to-python-threading/
Thank you for reply so I've do some research on threading and watched a few YouTube videos along with reading the article you've sent, however I am unable to implement it effectively in the code. it still wants to wait for the alarm to finish before returning to the top of the while true loop for me to carry out other tasks. do you have any ideas what I might be doing wrong?