0
What's wrong with my code(Python for Beginners)?
Its the search engine problem at the end of the Python for Beginners course. I've typed my code but only 3 test cases are correct, rest are not working. I've pasted my code below. text = input() word = input() def search(text, word): for i in text.split(): if i == word: return("Word found") else: return("Word not found") print(search(text, word))
2 Answers
+ 2
You don't need for loop and split function.
Your code can be simplified to this:
https://code.sololearn.com/cZ6xlypwi5Fq/?ref=app
+ 2
There is no need to iterate over 'word' variable. All we want to know is if there is an instance of 'text' in 'word', the if condition will suffice for this, hence you should remove the for loop.
A simpler way to structure your if condition would be:
if text in word:
return("Word found")
else:
return("Word not found")
The 'in' function is a simple tool to check if one string is present in another, which is ideal for this situation.
Hope this helps !