1

I'm new to Python and trying to make a sort of "Assistant" to get the hang of the language. The elif always starts if the if statement isn't satisfied, any idea how to fix it?

I already tried to change to all if, it didn't work.

import wikipedia

def getSummaryFromWikipedia(command):
    return wikipedia.summary(command, sentences=1)

def loopTest():
    command = input("Command: ")
    if "how to" in command:
        googleSearch(command)
    elif "who is" or "who was" in command:
        say(getSummaryFromWikipedia(command))
    else:
        print("Command not yet implemented.")

I want to set it up so that if "How to ..." is in the input it starts a google search, if there is "who is" or "who was" in the input to look it up on wikipedia and if none of the conditions are satisfied it just says that the command is not yet implemented. The problem is that if the first two if are satisfied the program works as intended, if I use some random word to try and get the else to work it just gives me wikipedia suggestions and some errors: "...\AppData\Local\Programs\Python\Python37\lib\site-packages\wikipedia\wikipedia.py:389: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently."

mypetlion
  • 2,415
  • 5
  • 18
  • 22

2 Answers2

3

"who is" or "who was" in command will always be true, because it's evaluated as "who is" or ("who was" in command) and a non-blank string evaluates as True. What you really want is "who is" in command or "who was" in command

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

The elif statement is checking two conditionals here.

  1. "who is" which evaluates as true as strings are truthy. As in if it's not null it is return true.

  2. "who was" in command, which is part of what you're trying to do.

You need to combine both of them together in one check otherwise it will always return true because of the truthy string. Like this:

elif "who is" in command or "who was" in command:
Tom Dee
  • 2,516
  • 4
  • 17
  • 25