Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While True loop help
#1
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
Reply
#2
you can create a new thread for the alarm to loop in
https://realpython.com/intro-to-python-threading/
Recommended Tutorials:
Reply
#3
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is using while True loop good? escape_freedom13 5 3,521 Jul-03-2020, 08:27 PM
Last Post: escape_freedom13
  Do break operators turn while loop conditions from True to False? Drone4four 5 2,947 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  How to use while true/while loop on python christing 4 2,878 Oct-08-2019, 08:02 AM
Last Post: perfringo
  Returning True or False vs. True or None trevorkavanaugh 6 9,234 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE
  file.write stops while true loop from executing in python3 boonr 3 3,101 Mar-25-2019, 12:50 PM
Last Post: ichabod801
  How to make loop go back to the beginning if test evaluaes to True. FWendeburg 1 2,821 Feb-13-2019, 01:26 AM
Last Post: stullis
  Returning true or false in a for loop bbop1232012 3 8,127 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  Get True of false outside a loop morgandebray 2 2,450 Aug-09-2018, 12:39 PM
Last Post: morgandebray

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020