user4714953
user4714953

Reputation:

Comparing just a part of the input. Python

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:'

Is there anyway that i can do so that i can use just part of the input, for example, if the question is 'Where are you from?' can i make it so that i compare just the 'are you from?' part of it?

Like: Suppose i ask Where are you from?? Or How old are you?

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."

I'm trying to create something like cleverbot

Upvotes: 0

Views: 31

Answers (2)

avinash pandey
avinash pandey

Reputation: 1381

You can also use the python re(regex) module in order to search for a substring

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

Upvotes: 0

use the in keyword:

Change:

if question == 'are you from':

to

if 'are you from' in question:

Upvotes: 1

Related Questions