仅比较输入的一部分。蟒蛇

时间:2015-03-27 02:53:36

标签: python python-2.7

while True:
    question = raw_input('Ask me a question: ')
    if question == 'Where are you from':
    print "I'm from Scandinavia, thanks for asking"
    elif question == 'How old are you':
        print "I'm 28, thanks for asking."
    elif question == 'What is your name:'

无论如何,我可以这样做,我只能使用部分输入,例如,如果问题是'你是哪里人来的?'我可以做到这一点,以便我只比较你是谁?'它的一部分?

像: 假设我问你是哪里人? 或者你多大了?

while True:
    question = raw_input('Ask me a question: ')
    if question == 'are you from':
        print "I'm from Scandinavia, thanks for asking"
    elif question == 'old':
        print "I'm 28, thanks for asking."

我试图创建类似cleverbot的东西

2 个答案:

答案 0 :(得分:1)

使用in关键字:

变化:

if question == 'are you from':

if 'are you from' in question:

答案 1 :(得分:0)

您还可以使用python re(regex)模块来搜索子字符串

import re
if re.search('are you from',question):
     #perform your logic here
elif re.search('old',question):
     #perform your logic here