我正在创建一个聊天机器人,当用户询问特定问题时,该聊天机器人应仅对用户做出响应,该机器人将检测到该代码并以正确的输出进行响应。但是我需要解决TypeError的帮助。
我的代码:
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.")
错误:
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
答案 0 :(得分:1)
该错误来自以下事实:您试图在返回打印语句的函数中进行迭代(使用in
)。我将其替换为基于输入的变量归因,现在可以使用了:
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.")
尽管这是一个聊天机器人,但我敢肯定,您还希望在重写user_input时进行循环,以便您可以计算新语句。像这样:
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.")