Python Forum

Full Version: input timer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I'm revising wake word and input for a project I'm working on I made this sample text so you can see what I'm trying to do basically two ways of waking it up.

The First Way:
you can say wake hi witch will wake it up and do the command all in one input it works

The Second Way:
this is where I'm having trouble so if you say wake it will wait for the command but i want a timer that if no input was given it will return to the top of the loop and wait for the wake word again. However the problem I'm running into is that once it returns to the top of the loop I have to hit enter or type something in before it will refresh the input so it can take the wake word. where am I going wrong and why is it doing this any help would be appreciated.
from threading import Timer


def inp():
    txt = input("cmd: ").lower()
    # txt = speech.record_audio().lower()
    return txt


def wake_word(x):
    wake_words = ['wake', ]  # list of wake words
    x = x.lower()  # converting text to all lower case words

    for phrase in wake_words:
        if phrase in x:
            find_length = len(phrase)
            return [True, find_length]
    return False


while True:

    try:
        timeout = 5
        t = Timer(timeout, print, ['Sorry, times up'])
        print("here")
        text = inp()
        response = ''
        if wake_word(text):

            if wake_word(text)[1] == len(text):  # if only wake word was given with no
                t.start()
                text = input("cmd: ")
                t.cancel()
            if "hi" in text:
                response = "hello"

        print(response)
    except Exception as e:
        print(e)