Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading