在我的聊天机器人里,我列了一个名为“问候”的列表。它包含标准的问候语,‘嗨’、‘你好’、‘怎么了’等等。我想让它输出‘怎么了?’用户的回答是“不多”或者别的什么,它的回答是“酷”之类的。我假设python列表(用[]标记)从0开始,在这种情况下是greetings[3]
,如下所示:
#setting up
greetings = ['Hi', 'Hello!', 'Greetings', 'What\'s up?', 'Good day', 'What\'s good?']
howru = ['Surprisingly well', 'Very good' , 'Good', 'I\'m doing well, you?' 'Not too good', 'Could be better', 'I\'m terrible today, thanks for asking']
dontknow = ['I don\'t understand', 'Say again?', 'I don\'t know about that one', 'I\'m sorry, I don\'t understand.']
def Bot():
print('Welcome to Almost Human, your (Almost) human friend.\nTry starting with a greeting!')
while(True):
a = input('You: ')
if a.lower() in('hi', 'hello', 'yo', 'what\'s up', 'greetings', 'wass good'):
botgreetings = random.choice(greetings)
print('AlmostHuman: ' + botgreetings)
if botgreetings == greetings[3]:
if a.lower() == 'not much':
print('AlmostHuman: Cool.')
elif a.lower() == 'the ceiling' or a.lower() == 'the sky':
print('AlmostHuman: You think you\'re funny, do you?')
else:
print('AlmostHuman: ' + random.choice(dontknow))
Bot()
然而,当它到了“怎么了”的时候,我加了“不多”,它会用“我不明白”来回应(因为它是else
到python
if a.lower() == blah
)。我试着把代码倒过来4个空格,这就解决了问题。但由于它现在是else
所在的位置,它的反应是:
AlmostHuman: Cool
AlmostHuman: I don't understand
当我输入“不多”。你知道吗
我认为这是一个缩进问题,但我不知道,我已经尝试了几个月来解决这个问题。你知道吗
这是我的第一个问题,请原谅我不够具体!:)
编辑:此处定义问候语:
#setting up
greetings = ['Hi', 'Hello!', 'Greetings', 'What\'s up?', 'Good day', 'What\'s good?']
howru = ['Surprisingly well', 'Very good' , 'Good', 'I\'m doing well, you?' 'Not too good', 'Could be better', 'I\'m terrible today, thanks for asking']
dontknow = ['I don\'t understand', 'Say again?', 'I don\'t know about that one', 'I\'m sorry, I don\'t understand.']
#bot begins here
def Bot():
以及其他名单。你知道吗
else:
在代码的最后,这里:
else:
print('AlmostHuman: ' + random.choice(dontknow))
Bot()
问题是你忘记了在
if botgreetings == greetings[3]:
之后要求更多的输入,所以如果你在这里说'not much'
,你实际上是在循环开始的时候对输入说了这句话(因为你已经失败了你的if
),而且not much
不是你预先定义的问候语的一部分-所以你最终得到了“未知问候语”的响应。你知道吗有了这些东西,它可以帮助你把你想要的行为画成一个state chart,当你错过了一个步骤时,视觉表现通常会让它变得更加明显。你知道吗
首先: 如果您遵循PEP8样式指南,那么使用
Bot()
会导致类名错误。你知道吗第二,您的输入将停止:
因为
not much
不在此列表中。你知道吗当您第二次检查a.lower()时,它仍然具有原始input()中的值,并且因为您第二次在顶级中检查它,所以它永远不会=='不多'
相关问题 更多 >
编程相关推荐