Python chatbot "TypeError: argument of type 'NoneType' is not iterable"

Question:

I’m creating a chatbot that should simply respond back to the user when the user asks it a specific question the bot will detect it the code and respond back with the correct output. But I need help resolving a TypeError.

My code:

user_name = input('''What would you like to be called: 
''')
bot_name = input('''
Now lets give you virtual bot a name: 
''')
print(' ')
print(f"{'Thank you'} {user_name} {'You have just summoned a new bot named'} {bot_name}!")

print('''

You may now have the permission to talk to the bot! HV

''')

import time
time.sleep(2)

import random
l = (bot_name + ":Hello!", bot_name + ":Hi!", bot_name + ":Hello!")
random_greeting = random.choice(l)
print(random_greeting)

def openinput(input):
    return print(input(f"{user_name}{':'}"))

if "how are you doing today" in openinput(input):
    print({bot_name} + 'Very Well! Thank you for asking :)')
elif "hi" in openinput(input):
    print('Hi!!')
else:
    print("ERROR1.0: It seem's like my index doesn't answer your question.")

The error:

Traceback (most recent call last):
  File "app.py", line 26, in <module>
    if "how are you doing today" in openinput(input):
TypeError: argument of type 'NoneType' is not iterable
Asked By: Castle

||

Answers:

The error comes from the fact that you are trying to iterate (using in) in a function that returns a print statement. I exchanged that for a variable attribution based on input and now it works:

user_name = input('''What would you like to be called: 
''')
bot_name = input('''
Now lets give you virtual bot a name: 
''')
print(' ')
print(f"{'Thank you'} {user_name} {'You have just summoned a new bot named'} {bot_name}!")

print('''

You may now have the permission to talk to the bot! HV

''')

import time
time.sleep(2)

import random
l = (bot_name + ":Hello!", bot_name + ":Hi!", bot_name + ":Hello!")
random_greeting = random.choice(l)
print(random_greeting)

user_input = input(f"{user_name}{':'}")

if "how are you doing today" in user_input:
    print({bot_name} + 'Very Well! Thank you for asking :)')
elif "hi" in user_input:
    print('Hi!!')
else:
    print("ERROR1.0: It seem's like my index doesn't answer your question.")

Since this is a chatbot though, I’m pretty sure you’ll also want to loop while rewriting user_input so you can compute new statements. Something like this:

while True:
    user_input = input(f"{user_name}{':'}")

    if "how are you doing today" in user_input:
        print({bot_name} + 'Very Well! Thank you for asking :)')
    elif "hi" in user_input:
        print('Hi!!')
    elif "bye" in user_input:
        print('Bye!')
        break
    else:
        print("ERROR1.0: It seem's like my index doesn't answer your question.")
Answered By: Telmo Trooper