所以我用 Python 写了一个简短的聊天机器人,但是有一个恼人的问题。posResponses()
程序最初只会调用该函数。
在上下文中,如果我用“悲伤”、“可怕”甚至“asdfasdfasdf”来回答它最初的问题,我仍然会得到积极的回应。
应该发生的是,如果我输入一个否定/模棱两可的关键字,应该调用negResponses()
/函数。ambiguousResponses()
事实并非如此。我做错了什么,我该如何解决?代码如下:
import random
import time
def opening():
print('Hello!')
def responseType():
responseType = str(input('How are you ?'))
if responseType == 'good' or 'great' or 'fantastic' or 'decent' or 'fine' or 'ok' or 'okay': posResponses()
elif responseType == 'bad' or 'terrible' or 'sad' or 'grumpy' or 'angry' or 'irritated' or 'tired': negResponses()
else: ambiguousResponses()
def posResponses():
number = random.randint(1, 4)
if number == 1:
print('That\'s great! So what\'s up?')
input()
ambiguousResponses()
if number == 2:
print('Really? I\'d like to hear more.')
input()
ambiguousResponses()
if number == 3:
print('I\'m glad to hear that. What\'s going on?')
input()
ambiguousResponses()
if number == 4:
print('Ah, me too. You should begin a topic discussion.')
input()
ambiguousResponses()
def negResponses():
number2 = random.randint(5, 8)
if number2 == 5:
print('That\'s really too bad. Care to elaborate?')
input()
ambiguousResponses()
if number2 == 6:
print('Awww. Why?')
input()
ambiguousResponses()
if number2 == 7:
print('That sucks! How come?')
input()
ambiguousResponses()
if number2 == 8:
print('What a shame! You should explain why.')
input()
ambiguousResponses()
def ambiguousResponses():
number = random.randint(1, 4)
if number == 1:
print('Interesting. Carry on.')
input()
ambiguousResponses()
if number == 2:
print('Wow, elaborate!')
input()
ambiguousResponses()
if number == 3:
print('What an astute remark! Continue.')
input()
ambiguousResponses()
if number == 4:
print('How interesting. Please do explain further.')
input()
ambiguousResponses()
if __name__ == "__main__":
opening()
responseType()