PY Chatbot 重复用户输入提示

PY Chatbot Repeating user input prompt

这里是 Python 的新手。我编写一个小型聊天机器人已经有一段时间了,当我提示用户输入内容而不是回答正确的响应时,机器人只是重复提示。之前的一切工作正常,但它陷入了一个要求用户输入的循环。

print("Hello " + User_Name + """. 
Tonic is a simple Python chatbot made in order to test things such as boolean logic
and variable definition.
(Also remember to speak to the bot in lowercase, without punctuation.)""")
while 1 == 1:
str1 = input("Say something: ")
#"hello" //////////////////////////////////////////
if "hello" in str1:
print("Hi " + User_Name + "! (I read this as the 'hello' greeting.");
#"hi" //////////////////////////////////////////
if "hi" in str1:
print("Hi " + User_Name + "! (I read this as the 'hi' greeting.");
#"how are you?" //////////////////////////////////////////
if "how are you" in str1:
print("good, how about you? (I read this as you asking 'how are you'.)")
mood = input("Enter Mood: ")
if "good" in mood:
   print("Nice to hear " + User_Name + "! (I read this as you being in a good mood.)");
if "bad" in mood:
   print("I hope you feel better soon, " + User_Name + "! (I read this as you being in a bad mood.)");
#"name length" //////////////////////////////////////////
if "name length" in str1:
print( "Your name is " + len(User_Name) + "letters long. (I read this as you asking how long your name is.");```

您的 while 循环没有退出条件。它将永远重复,因为 1 总是等于 1。 相反,您可以执行 while (str1 != "quit"),这将在用户在提示中输入“退出”后停止 while 循环。

另外,旁注,您不应该在 Python.

中的行尾使用分号
print("Hello " + User_Name + """. 
Tonic is a simple Python chatbot made in order to test things such as boolean logic
and variable definition.
(Also remember to speak to the bot in lowercase, without punctuation.)""")
while (str1 != "quit"): # You need an exit condition here
    str1 = input("Say something: ")
    #"hello" //////////////////////////////////////////
    if "hello" in str1:
        print("Hi " + User_Name + "! (I read this as the 'hello' greeting.")
    #"hi" //////////////////////////////////////////
    if "hi" in str1:
        print("Hi " + User_Name + "! (I read this as the 'hi' greeting.")
    #"how are you?" //////////////////////////////////////////
    if "how are you" in str1:
        print("good, how about you? (I read this as you asking 'how are you'.)")
        mood = input("Enter Mood: ")
        if "good" in mood:
            print("Nice to hear " + User_Name + "! (I read this as you being in a good mood.)")
        if "bad" in mood:
            print("I hope you feel better soon, " + User_Name + "! (I read this as you being in a bad mood.)")
    #"name length" //////////////////////////////////////////
    if "name length" in str1:
        print( "Your name is " + len(User_Name) + "letters long. (I read this as you asking how long your name is.")
  1. 修正缩进
  2. 将 len 的输出转换为字符串
print("Hello " + User_Name + """. 
Tonic is a simple Python chatbot made in order to test things such as boolean logic
and variable definition.
(Also remember to speak to the bot in lowercase, without punctuation.)""")
while 1 == 1:
    str1 = input("Say something: ")
    #"hello" //////////////////////////////////////////
    if "hello" in str1:
        print("Hi " + User_Name + "! (I read this as the 'hello' greeting.");
        
    #"hi" //////////////////////////////////////////
    if "hi" in str1:
        print("Hi " + User_Name + "! (I read this as the 'hi' greeting.");
    #"how are you?" //////////////////////////////////////////
    if "how are you" in str1:
        print("good, how about you? (I read this as you asking 'how are you'.)")
    mood = input("Enter Mood: ")
    if "good" in mood:
       print("Nice to hear " + User_Name + "! (I read this as you being in a good mood.)");
    if "bad" in mood:
       print("I hope you feel better soon, " + User_Name + "! (I read this as you being in a bad mood.)");
    #"name length" //////////////////////////////////////////
    if "name length" in str1:
        print( "Your name is " + str(len(User_Name)) + "letters long. (I read this as you asking how long your name is.");

我刚刚找到了一个模块,您只需几行代码就可以创建一个聊天机器人 参考这个 https://pypi.org/project/prsaw/

from prsaw import RandomStuff

random = RandomStuffV2()

str1 = input("message: ")

response = random.get_ai_response(str1)
print(response)