-2

I am new to python, I have been trying to use the text to speech module it works fine but when I try to add an OR operator to my if..elif..else block the program discards everything below the elif statement that contains the OR operator, any help will be greatly appreciated. Code provided below

def response(text):
   if text != "":
        if 'hello jarvis' in text:
            print("hello sir, how can i help you")
            speak("hello sir, how can i help you")
        
        elif 'how are you' in text:
            print("I'm fine sir, how are you?")
            speak("I'm fine sir, how are you?")

        elif 'are you alive' in text:
            print("no i am not alive, i am just a combination of very complex algorithms that keeps me functional")
            speak("no i am not alive, i am just a combination of very complex algorithms that keeps me functional")
            

        elif 'what is the time' or 'what time is it' in text:
            strtime = datetime.today().strftime("%H:%M:%p")
            print(f"the time is {strtime}")
            speak(f"the time is {strtime}")

        elif 'thank you' in text:
            speak("you are welcome sir")
            exit()

        else:
            url = f"https://www.google.com/search?q={text}"
            webbrowser.get().open(url)
            speak("here is what i found")

   else:
     speak("call me when you have something to ask me")

  • In which elif statement are you trying to add the or operator? What do you mean by it breaks? – EHM Aug 06 '22 at 19:54
  • 1
    try `elif 'what is the time' in text or 'what time is it' in text:` – Michael S. Aug 06 '22 at 19:55
  • on the third elif statement there is an OR operator, which is supposed to check whether a user said one of the two phrases and give response which works fine but when a user says a phrase that is not present on the if block the else statement is supposed to be executed but that doesn't happen it keeps going back to the third elif statement. – Mundheer Suleiman Aug 06 '22 at 19:59
  • Thank you so much @MichaelS. , i completely missed the in text part i think i need to rest for a while i have been at it for four hours now. – Mundheer Suleiman Aug 06 '22 at 20:02

2 Answers2

1

The issue is that 'what is the time' per se is regarded as True, thus the branch is always taken. Simply change

elif 'what is the time' or 'what time is it' in text:

to

elif 'what is the time' in text or 'what time is it' in text:
Michael Hodel
  • 2,845
  • 1
  • 5
  • 10
1

Try this:

elif 'what is the time' in text or 'what time is it' in text:
            strtime = datetime.today().strftime("%H:%M:%p")
            print(f"the time is {strtime}")
            speak(f"the time is {strtime}")

The reason for this is because in your original code you're seeing if ('what is the time') evaluates to true and then if ('what time is it' in text) evaluates to true, you aren't comparing them together. The first comparison doesn't make sense because strings can't evaluate to boolean values.

BVB44
  • 266
  • 2
  • 11
  • 1
    yeah i already figured it out i missed the in text on the first condition, i think i need to rest for a while i have been pushing myself too hard. – Mundheer Suleiman Aug 06 '22 at 20:06