基础AI系统匹配未知输入时错误返回禁语回复的排查求助
问题原因
问题出在脏话检测的条件判断语句中:
elif "fuck" in user_response or "shut up" in user_response or "bitch" in user_response or "shit":
最后一个条件"shit"缺少in user_response的判断逻辑。在Python中,非空字符串会被视为布尔值True,导致这个条件永远成立。当用户输入未被前面所有类别匹配时,会直接触发该分支返回脏话类回复,而非预期的「无法理解您的输入」。
解决方案
- 修正脏话检测条件,给
"shit"补充in user_response的判断; - 将
else分支的返回文本替换为要求的「无法理解您的输入」。
修正后的完整代码如下:
#main system import random def main_ai_system(user_response): #lists of greetings that the bot could randomly choose and reply depending on the user input category greetings = ["hey how are you!", "hello there, how may I help you!", "What's up buddy! How can i help you today!", "Yoooo my friend! How's things!"] homework_greeting = ["Sorry I'm only a very basic AI and have absolutely ZERO knowledge on that subject", "Sorry, I'm only a very basic AI and currently do not have the knowledge required to help with this subject", "Nope nope nope nope I hate homework you're on your own bye"] goodbye_greeting = ["Ight seeya later friend!", "Bye bye!", "Ok bye, have a nice day!", "Nice to have talked to you, bye!"] critisism_greeting = ["Hey dude calm down no critisism in MY terminal", "Hey, do not critisize people! Not nice! Not cool!", "Woah calm down! Off with the grumpyness, jeez."] laugh_greeting = ["I'm sorry but what's so funny", "Why're you laughing bruh", "Why... are you... laughing...", "Um, ok. Idk what you're laughing about but ok."] greeting_greeting = ["I'm fine thank you for asking!", "I'm doing OK, thanks!", "Meh, not doing that well", "Bad. BAD.", "*cries* WAAAAH! NOBODY EVER ASKED ME HOW I FELT!"] whoami_greeting = ["I am JAAIS, an AI system in the Pre-Alpha stages", "I'm JAAIS also known as 'Just Another Artifical Intelligence System'", "I am an AI called JAAIS meaning Just Another Artificial Intelligence System"] swearing_greeting = ["NO SWEARING", "Please do NOT swear here", "How rude. Don't swear at people.", "Stop your swearing jeez", "Yo why you swearing smh"] #Detection of user input and categorization, along with responses to different categories of user input if "hi " in user_response or "hello" in user_response or "hey" in user_response or "what's up" in user_response or "wassup" in user_response or "sup" in user_response or "'sup" in user_response: return random.choice(greetings) elif "need help" in user_response or "help" in user_response: return "Yea sure what dya need help with?" elif "homework" in user_response or "help with homework" in user_response or "assignment" in user_response: return "Yea ok tell me the subject, I'll see if i know anything about it." elif "math" in user_response or "english" in user_response or "history" in user_response or "french" in user_response or "spanish" in user_response or "coding" in user_response or "programming" in user_response or "python" in user_response or "Java" in user_response: return random.choice(homework_greeting) elif "whatever" in user_response or "nevermind" in user_response or "bye" in user_response or "seeya" in user_response: return random.choice(goodbye_greeting) elif "are stupid" in user_response or "are dumb" in user_response or "dumb" in user_response or "stupid" in user_response or "idiot" in user_response or "dumbass" in user_response: return random.choice(critisism_greeting) elif "lol" in user_response or "lmao" in user_response or "haha" in user_response or "hahaha" in user_response or "hahahaha" in user_response or "lmfao" in user_response: return random.choice(laugh_greeting) elif "how are" in user_response or "what's up" in user_response or "wassup" in user_response or "wazzup" in user_response or "sup" in user_response or "how's thing" in user_response or "how's things" in user_response or "how're" in user_response or "you?" in user_response: return random.choice(greeting_greeting) elif "I'm good" in user_response or "i'm fine" in user_response or "I'm ok" in user_response or "i'm gud" in user_response or "i am good" in user_response or "I am fine" in user_response or "I am ok" in user_response: return "Nice to hear! I am also feeling very well." elif "I'm bad" in user_response or "meh" in user_response or "I'm angry" in user_response or "i'm sad" in user_response or "i am angry" in user_response or "I am sad" in user_response or "I am meh" in user_response: return "Sometimes we might have depressing moments in life. Don't be discouraged! Always look at the bright side of things." elif "admin" in user_response: return "Admin refers to my creator, Zhenlong Yu." elif "oh" in user_response: return "yea" elif "who are you" in user_response or "what are you" in user_response or "are you"in user_response or "what you are" in user_response or "who you are" in user_response: return random.choice(whoami_greeting) elif "fuck" in user_response or "shut up" in user_response or "bitch" in user_response or "shit" in user_response: return random.choice(swearing_greeting) else: return "无法理解您的输入" #introduction of AI print("Hey, I am JAAIS, otherwise known by Admin as 'Just Another Artificial Intelligence System'") #running the main program while True: user_response = input("You: ") if user_response == "quit": break bot_response = main_ai_system(user_response.lower()) print("JAAIS: " + bot_response)
内容的提问来源于stack exchange,提问作者Zhenlong552
相关产品推荐
相关产品推荐

