+ 4

How do I fix this code?

I want the program to detect at least 1 out of 2 possible elements in a sentence so it can determine that it is a question, before it shoots out an appropriate answer. import re, random questions = ['do you', 'did you'] question_sign = ['?'] yes_no = ['yes', 'no'] confused = ["I don't know what you mean!"] pattern1 = re.compile('|'.join(r'(\b' + question + r'\b)' for question in questions)) pattern2 = re.compile('|'.join(r'(\b' + questionsign + r'\b)' for questionsign in question_sign)) if pattern1.search(input().lower() in questions) or pattern2.search(input().lower()) in question_sign: print (random.choice(yes_no)) else: print(random.choice(confused))

8th Mar 2019, 7:15 AM
Ядрото
Ядрото - avatar
2 Respuestas
+ 4
No need to go for regexp here, python has nice builtin functions to deal with this. sentence = input().lower() is_question = any(sentence.endswith(s) for s in question_sign) or any(sentence.startswith(s) for s in questions) You need to save the input once in a variable, otherwise the program will expect a new input every time you compare. https://code.sololearn.com/c7I8O5wP4JRW/?ref=app
9th Mar 2019, 3:47 AM
Tibor Santa
Tibor Santa - avatar