+ 1
(pls help)python search engine
You’re working on a search engine. The given code takes a text and a word as input and passes them to a function called search(). The search() function should return "Word found" if the word is present in the text, or "Word not found", if it’s not. Sample Input "This is awesome" "awesome" Sample Output Word found
7 Answers
+ 7
You are printing the return value of your search function and you are not returning a value, so its None. Instead of calling print in your search function you could return the string.
e.g.:
...
return "Word not found"
...
btw it could be better to return a boolean value and handle the print outside depending on the return value.
+ 2
M X , print of print function returns None. Instead =>
text= input()
word= input()
def search(text,word):
if word in text:
return 'Word found'
else:
return 'Word not found'
print(search(text,word))
Hope it helps you.
+ 1
TheWh¡teCat 🇧🇬 Pls avoid giving finished code as answer, because it makes the OP skip the most important part of learning. Prefer giving hints for the OP to find the solution instead.
0
My code:
text= input()
word= input()
def search(text,word):
if word in text:
print('Word found')
else:
print('Word not found')
print(search(text,word))
My issue: Why my code keeps outputting: word found/not found + none.
0
Emerson Prado , that's not the case here. M X posted his attempt, so I pointed his errors. I didn't give him the ready solution. When I try to help someone here, I always ask for his/her attempt. So I completely disagree with your post (I just mentioned his errors, according to his code).
0
TheWh¡teCat 🇧🇬 I mean the presence of the finished code, with the solution already implemented, in your answer. My recommendation was, instead, giving an explanation of it, then leave the imolementation for the OP.
0
Emerson Prado , I think it's normal to write it, to show the errors. I didn't make any changes in logic of M X 's code and didn't use any other algorithm. The whole thing is that I replaced print function in search function with return statement to point his/her mistakes. So there's no major things to explain or to implement. Completely natural explanation, in my opinion, like many others I gave in Q&A section.