Search code examples
functionwhile-loopartificial-intelligence

Python Virtual Assistant not continuing after a block of code. while 1:


I will provide the code for a portion. It doesn't continue after using commands within a while block. I think it is the code block text = get_audio().lower() that is giving me the error.

def process_text():
pass

if __name__ == "__main__":
assistant_speaks('''Hello, I am a Virtual Assistant. 
                I am here to make your life easier. You can command me to perform 
                various tasks such as calculating sums or opening applications. 
                Please tell me who you are.''')
name = 'are'
name = get_audio()
assistant_speaks("Hello, " + name + '.')

while 1:

    assistant_speaks("What can i do for you?" + name + '.')
    text = get_audio().lower()

    if text == 0:
        continue

    if "exit" in str(text) or "bye" in str(text) or "sleep" in str(text):
        assistant_speaks("Ok bye, " + name + '.')
        break

    # calling process text to process the query
        process_text()


    def process_text():
        try:
            if 'search' in input or 'play' in input:
                # a basic web crawler using selenium
                search_web(input)
                return

            elif "who are you" in input or "define yourself" in input:
                speak = '''Hello, I am a virtual assistant. 
                I am here to make your life easier. You can command me to perform 
                various tasks such as calculating sums or opening applications.'''
                assistant_speaks(speak)
                return

Basically, it doesn't answer any questions when I ask the elifs. Past the try:

Does anyone have any ideas?


Solution

  • There are at least three mistakes you should correct in order to get the behavior you want from your program:

    • Replace input with text inside the try block. In Python, input is a built-in function, not a string. You already assigned the user's answer to the variable text, so why not use that one? :)
    • Define your process_text function before you call it. Practically, you should move the line where you call process_text() somewhere after the definition of that function.
    • There are also a couple of indentation errors, and the most important happens when you call the process_text() function: you should un-indent that line back one level, otherwise it will be executed inside the if "exit" in ... block.

    You should also define process_text before the while loop, because you just need to define it once.