[英]Python chatbot “TypeError: argument of type 'NoneType' is not iterable”
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.
但我需要解决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
The error comes from the fact that you are trying to iterate (using in
) in a function that returns a print statement. 该错误来自于您试图在返回打印语句的函数中进行迭代(
in
中使用)的事实。 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. 由于这是一个聊天机器人,因此我很确定您在重写user_input时也要循环,以便您可以计算新语句。 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.")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.