How do i ignore a the rest of a sentence that contains a word i want to print?

I’m trying to built an assistant, at first while I say "hi" it returns "hi" working great.
but if I say "hi, what time is it" it goes to the first if cause it finds the value "hi" in my sentence.

This is a piece of my code:

def run_alexa():
    command = take_command()
    matches_hi = ['hey', 'hello', 'hi there']
    if any(x in command for x in matches_hi):
        talk(random.choice(matches_hi))
        print()

     if 'what time' in command:
        time = datetime.datetime.now().strftime('%H:%M:')
        talk('The current time is' + time)`

command is the variable that contains my speech
I’m trying to make the code ignore the first if (without using elif in that cause cause it will not let me run ‘elif any’) while I’m saying "hello, what time is it?" or whatever.

>Solution :

You can try splitting the response, if the length of the response is one word it will run the first if statement, if the length exceeds 1 then it will continue to the second if statement.

def run_alexa():
        command = take_command()
        matches_hi = ['hey', 'hello', 'hi there']
        if any(x in command for x in matches_hi) and len(command.split(" ")) > 1:
            talk(random.choice(matches_hi))
            print()

        if 'what time' in command:
            time = datetime.datetime.now().strftime('%H:%M:')
            talk('The current time is' + time)

Leave a Reply