+ 3
How to do this Python ?
You’re working on a search engine. Watch your back Google! 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
18 ответов
+ 15
What have you tried so far?
+ 7
def seacrh(text,word) :
if word in text :
return 'Word found'
else:
return 'Word not found'
text = input()
word = input()
print(seacrh(text,word))
+ 5
Please post your code in the code playground and post a link here. That way we can take a look and tell you what you had wrong.
You don't really learn by getting the solution from one of us.
+ 3
It's a bug.
Try again later
https://www.sololearn.com/discuss/2845292/?ref=app
+ 2
Jayashree Das
Working fine. No issue.
+ 2
Simon Sauter
Either you return or just print. Both are fine. Test case doens't depends on how your function is.
Yes there is typo but code is working.
+ 2
def search(text,sentance):
if text in sentance :
print("\nWord Found!")
else :
print("\nWord Not Found!")
sentance = input("Enter the sentance : ")
text = input("Enter Text : ")
search(text,sentance)
+ 2
Simon Sauter check the output man
+ 2
Simon Sauter ok
+ 1
Yes I tried but the ans. Is error
Plz tell what is it's correct ans.
+ 1
def seacrh(text,word) :
if word in text :
print ('Word found')
else:
print('Word not found')
text = input()
word = input()
seacrh(text,word)
+ 1
I think the problem is that the task is to write a function that *returns* the answers, whereas in your code it *prints* them.
Replace "print" with "return" inside the function definition and put a print statement in front of the function call.
Also, there is a typo in the last line: it should be "search", not "seacrh".
+ 1
Line 5
Invalid syntax
this is the essue
+ 1
Jayashree Das
I just copied your code and worked fine. No invalid syntax.
Now try this code:
def search(text, word):
if word in text :
print ('Word found')
else:
print ('Word not found')
text = input()
word = input()
search(text, word)
+ 1
Jayashree Das How about this one:
search = lambda x, y: "Word" + " not" * (y not in t) + " found"
# Hope this helps
+ 1
A͢J You can pass the test cases using "print" in the function. But that's just because the test cases only test whether your code produces the correct output and not how you generate that output. But to actually perform the task (and to learn the actual lesson) you have to use "return".
Using "print" inside the function is basically cheating. You can also pass the test cases by using the re module's search function instead of writing your own function.
+ 1
Aditya Nijave Your code generates the correct output. But it does not generate the output in the way the task demands. Read my answer right above yours.
This is an important difference. Writing functions that return values instead of just printing them is a very common task you need to be able to master.
0
Aditya Nijave why did you post that? It doesn't really add anything that hasn't already been posted and it doesn't perform the task.