2

所以我用 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()
4

2 回答 2

6

Python 是这样解析的:

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()

'great'是一个真值,所以这个if语句总是成功的。你可能想像这样重写它:

if responseType == 'good' or responseType == 'great' or responseType == 'fantastic' or responseType == 'decent' or responseType == 'fine' or responseType == 'ok' or responseType == 'okay':
    posResponses()
elif responseType == 'bad' or responseType == 'terrible' or responseType == 'sad' or responseType == 'grumpy' or responseType == 'angry' or responseType == 'irritated' or responseType == 'tired':
    negResponses()
else:
    ambiguousResponses()

或者,最好:

if responseType in {'good', 'great', 'fantastic', 'decent', 'fine', 'ok', 'okay'}:
    posResponses()
elif responseType in {'bad', 'terrible', 'sad', 'grumpy', 'angry', 'irritated', 'tired'}:
    negResponses()

您甚至可以将这些响应移动到一个常量中:

# near the top of the file:
GOOD_RESPONSES = {'good', 'great', 'fantastic', 'decent', 'fine', 'ok', 'okay'}
BAD_RESPONSES = {'bad', 'terrible', 'sad', 'grumpy', 'angry', 'irritated', 'tired'}

# when you need to judge a response:
if responseType in GOOD_RESPONSES:
    posResponses()
elif responseType in BAD_RESPONSES:
    negResponses()
else:
    ambiguousResponses()
于 2012-08-13T01:32:14.640 回答
0

posResponses只被调用一次,因为在 posResponses 中你调用了 ambiguousResponses,而在 ambiguousResponses 中你调用了 ambiguousResponses。所以它永远不会退出 ambiguousResponses,它只会继续调用 ambiguousResponses 直到你用完堆栈并且程序崩溃。

你试图让这个程序递归。不应该,应该有一个以 input() 调用为中心的循环,整个程序中只需要一个循环,而不是 15 或任何你现在拥有的循环。

但比这更明显的错误是,您对响应是正面还是负面的测试不包括在递归中,因此程序只检查一次输出的响应。

于 2012-08-13T03:17:19.880 回答