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

Question:

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.

Asked By: Nir Malka

||

Answers:

One way to achieve this is by using regular expressions to match the specific pattern of the command. You can use the re module in Python to match the specific pattern of the command and then execute the corresponding code block.

import re

def run_alexa():
    command = take_command()
    matches_hi = ['hey', 'hello', 'hi there']
    if re.match(r'^(' + '|'.join(matches_hi) + ')b', command):
        talk(random.choice(matches_hi))
        print()
    elif re.match(r'^(' + '|'.join(matches_hi) + ')b.*what time.*', command):
        time = datetime.datetime.now().strftime('%H:%M:')
        talk('The current time is' + time)
Answered By: SuperStew

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)
Answered By: Flewitt

Firstly, if you need to check if string is present in a list, instead of doing it like this if any(x in command for x in matches_hi):

You can use:

if command in maches_hi:

Secondly if you need to perform additional tasks after seeing a hi in the message, you can nest statements. The pseudo-code would be something like:

if command in ['hi', 'hello', 'hi there']:
    if 'what time' in command:
        do_some_things()
Answered By: user15144596

without using elif in that cause cause it will not let me run ‘elif any’

I’m not sure what problem you are getting but the code runs fine for me (Python 3.10.9) with the elif:

def run_alexa():
    command = take_command()
    matches_hi = ['hey', 'hello', 'hi there']

     if 'what time' in command:
        time = datetime.datetime.now().strftime('%H:%M:')
        talk('The current time is' + time)
    elif any(x in command for x in matches_hi):
        talk(random.choice(matches_hi))
        print()

If for whatever reason you really don’t want to use an elif you could also just return from the function if the first if condition matches:

def run_alexa():
    command = take_command()
    matches_hi = ['hey', 'hello', 'hi there']

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

    if any(x in command for x in matches_hi):
        talk(random.choice(matches_hi))
        print()
Answered By: moriarty