+ 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))

27th Feb 2023, 1:40 AM
The Tina Experience
The Tina Experience - avatar
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 :)
27th Feb 2023, 3:07 AM
Mozzy
Mozzy - avatar
+ 2
Thanks Mozzy
27th Feb 2023, 7:48 AM
The Tina Experience
The Tina Experience - avatar
+ 1
text == word and word != text is always false, and text == text is always true so it will always print Word found.
28th Feb 2023, 12:22 PM
bcer
bcer - avatar
0
bcer: good answer
28th Feb 2023, 2:57 PM
The Tina Experience
The Tina Experience - avatar