-2

I am trying to make a sort-of AI thing with Python, but I've got stuck on some part.

When the AI says something, (says hello at the beginning), it gives you the ability to input text, once you've entered that, it would go through if 'keyword' in answer statements to provide a reply. Once it replies, it calls the AImain() function, restarting that process. I can't seem to get it to restart, as compiling would just say hello initially (as intended), but give no input text ability, which results in the script ending. Here's the code:

def AImain():
    response = input("YOU:")

    if 'Hello' or 'Hi' in answer:
        print(random.choice(welcomeMessages))
        AImain()

I'm new to python and I don't understand why this isn't working. Thanks!

Prithvi Raj
  • 1,611
  • 1
  • 14
  • 33
  • 1
    The input variable is in response but you are testing the value of answer. – Eric Truett Jun 05 '21 at 17:08
  • 1
    1) Please fix your indentation. We can only guess what you meant to write. 2) `'Hello' or 'Hi' in answer` is equivalent to `'Hello' or ('Hi' in answer)`, which will always evaulate to `True`. See [How to test multiple variables against a single value?](/questions/15112125/) 3) you never use `response`, and we don't see how `answer` is defined. You seem to have these variables mixed-up. 4) You almost certainly do not want to use recursion here, since you risk reaching the [maximum recursion depth](/questions/3323001/). – Brian61354270 Jun 05 '21 at 17:09

1 Answers1

0

Your code has an indentation problem at the Aimain() function and we can't see how answer is declared. You are assigning the input to response but reading answer in your code.

Here is my suggestion to your code


    response = "Hi" #you can eliminate this, assuming response is declared somewhere else
    
    def AImain():
        global response
        response = input("You:")
    
    while response == 'Hello' or response == "Hi":
        print(random.choice(welcomeMessages))
        AImain()

FSchieber
  • 26
  • 1
  • 6