簡體   English   中英

Python聊天機器人“ TypeError:'NoneType'類型的參數不可迭代”

[英]Python chatbot “TypeError: argument of type 'NoneType' is not iterable”

我正在創建一個聊天機器人,當用戶問一個特定問題時,它應該簡單地回復用戶,該機器人將檢測到該代碼並以正確的輸出進行回復。 但我需要解決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

該錯誤來自於您試圖在返回打印語句的函數中進行迭代( 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.")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM