调用了错误的函数?

时间:2012-08-13 01:28:36

标签: python python-3.x

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

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

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

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

但是比这更明显错误,你的反应测试是否包含在递归中,因此程序只检查输出的响应一次。