Answer the question
In order to leave comments, you need to log in
How to solve problem with output condition and regular expressions in Python?
import re
m_list = ["yellow\?", "red\?", "blue\?"]
data = input().lower()
a = re.search("black\?", data)
print("Yes occurrence" if a else "No occurrence")
1) I'm trying to make print not output the else block and print only the value when the regular expression pattern matches. Attempts to remove the else block, or to replace "No entry" with pass or None - give an error.
2) How to pass the m_list array to the re.search conditions so that the regular expression checks the text against all elements specified in m_list?
Answer the question
In order to leave comments, you need to log in
one)
Attempts to remove the else block, or to replace "No entry" with pass or None - give an error.
a if q else b
is a ternary operator. It has three arguments (a, b, q) and the result is either a or b depending on whether q is true . Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question