我正在创建一个聊天机器人,当用户询问特定问题时,它应该简单地回复用户,机器人将检测到代码并以正确的输出进行回复。但我需要帮助解决类型错误。
我的代码:
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
该错误来自于您尝试在返回 print 语句的函数中进行迭代(使用
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.")