0

I am trying to make a voice assistant. There is lots of problem with it. But this time I am facing very strange problem in if statement. My code:

greeting()   # greeting mesaage
ear = listen() # listen my voice stored in ear variable
mind = str(recognise(ear)) #recogniser convert it in text by help of google and store in mind variable
        
if "what"and"about"and"you" in mind:
    speak(" i am also fine!")
    speak("what can i do for you?")
elif "information" in mind:
    speak("what you want to know from me, sir!")

Whenever I speak one of the word "what" or "about" or "you" even with "information" it executes if statement rather than elif statement. While as per my understanding it must be pass from if statement whenever this 3 words are in same string. so I am confused what is the mistake in the code.

James Z
  • 12,209
  • 10
  • 24
  • 44
Rahul
  • 13
  • 4

2 Answers2

0

This line -

if "what"and"about"and"you" in mind:
        speak(" i am also fine!")
        speak("what can i do for you?")

is not what you think it is. You need to add in for each string -

if "what" in mind and "about" in mind and "you" in mind:
        speak(" i am also fine!")
        speak("what can i do for you?")

Basically, in the earlier if statement, the statement translates to - if true and true and 'you' in mind, which is quite different than the logic you want.

PCM
  • 2,881
  • 2
  • 8
  • 30
  • Thank you so much PCM for helping. Problem has been resolved and now I go ahead for learning more on python. Thanks to Stack overflow for making such a kind helpful platform. – Rahul Sep 19 '21 at 02:55
0

Depending on how big your list of words gets, you could get fancy and do something like this:

words = ["what", "about", "you"]
mind = "That's what I like about you"
if [word for word in words if word in mind]:
    speak(" i am also fine!")
    speak("what can i do for you?")
djs
  • 3,947
  • 3
  • 14
  • 28