+ 1
There is something that I am missing. What is wrong with this code?
text = input() word = input() def search(text, word): if text == word and word != text: return "Word found" elif text == text: return "Word found" elif word == word: return "Word found" else: return "Word not found" print(search(text, word))
4 Answers
+ 4
The Tina Experience
It might help if you explain what the search function is meant to do. I assume it returns 'Word found' if the word is in the text, and 'Word not found' otherwise. If this is true, the function's logic doesn't really work.
The first if condition will always be False, since `text==word` and `word!=text` are logical opposites: 1*0 = 0*1 = 0. The next elif condition `text==text` will always be True for obvious reasons. So the function always returns "Word found" regardless of the input.
The solution is a lot simpler. You can use Python's `in` operator to check if a substring is in another string. In your function, try this instead:
if word in text:
return "Word found"
else
return "Word not found"
Hope this helps :)
+ 2
Thanks Mozzy
+ 1
text == word and word != text is always false, and text == text is always true so it will always print Word found.
0
bcer: good answer