1

I am making a bot like a AI in python

In this code I want any condition to run when I entered a input.

Whenever a am typing anything I am expecting to else: to be true but everytime the 3rd elif is being true like it's an else

I had tried changing position of the elif statment so some other statement is executing

Actually my code is very big it's just a part from it

No error msg for indentation error:

The code starts from here:


if "time now" in command:
        print(prefix , time)

elif "date today" in command:
        print(prefix ,  date)
    
elif "quit" or "exit" in command:
        print(prefix , "Good bye user !!! ")
        print("0====[]::::::::::>")
        exit()
else:
        print(prefix, "I am unable to understand you ): ")
        print( "Type the command correctly ! !")
        print()
        return AQassistant()

3 Answers3

1
if "quit":
    print("True")

Output: True

Your elif should instead be: elif "quit" in command or "exit" in command

Otherwise the condition will always evaluate to true.

Alternatively, you can do:

command = "quit"

if any(cmd in command for cmd in ["quit", "exit"]):
    print("quit/exit")
ptts
  • 1,848
  • 6
  • 14
0

This:

elif "quit" or "exit" in command:

is same as writing

elif ("quit") or ("exit" in command):

Due to Operator precedence - in is more sticky than or. As "quit" is non-empty str it is considered to be truthy in python, so above is always True as one part of or is True.

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

"quit" is always True!

"quit" or "exit" in command is the same as True or "exit" in command. The same as simply True.

Any empty string is False

All other strings are True

So, your code should be something like this:

if "time now" in command:
        print(prefix , time)

elif "date today" in command:
        print(prefix ,  date)
    
elif "quit" in command or "exit" in command:
        print(prefix , "Good bye user !!! ")
        print("0====[]::::::::::>")
        exit()
else:
        print(prefix, "I am unable to understand you ): ")
        print( "Type the command correctly ! !")
        print()
        return AQassistant()