How to do this Python ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

28th Jul 2021, 4:31 AM
Jayashree Das
Jayashree Das - avatar
18 Answers
+ 15
What have you tried so far?
28th Jul 2021, 4:42 AM
David Ashton
David Ashton - avatar
+ 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))
28th Jul 2021, 6:05 AM
Oma Falk
Oma Falk - avatar
+ 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.
28th Jul 2021, 4:59 AM
Simon Sauter
Simon Sauter - avatar
28th Jul 2021, 7:56 AM
Simba
Simba - avatar
+ 2
Jayashree Das Working fine. No issue.
28th Jul 2021, 5:20 AM
A͢J
A͢J - avatar
+ 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.
28th Jul 2021, 6:03 AM
A͢J
A͢J - avatar
+ 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)
29th Jul 2021, 4:39 PM
Aditya Nijave
Aditya  Nijave - avatar
+ 2
Simon Sauter check the output man
29th Jul 2021, 4:43 PM
Aditya Nijave
Aditya  Nijave - avatar
29th Jul 2021, 4:47 PM
Aditya Nijave
Aditya  Nijave - avatar
+ 1
Yes I tried but the ans. Is error Plz tell what is it's correct ans.
28th Jul 2021, 4:47 AM
Jayashree Das
Jayashree Das - avatar
+ 1
def seacrh(text,word) : if word in text : print ('Word found') else: print('Word not found') text = input() word = input() seacrh(text,word)
28th Jul 2021, 5:07 AM
Jayashree Das
Jayashree Das - avatar
+ 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".
28th Jul 2021, 5:49 AM
Simon Sauter
Simon Sauter - avatar
+ 1
Line 5 Invalid syntax this is the essue
28th Jul 2021, 6:17 AM
Jayashree Das
Jayashree Das - avatar
+ 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)
28th Jul 2021, 8:28 AM
A͢J
A͢J - avatar
+ 1
Jayashree Das How about this one: search = lambda x, y: "Word" + " not" * (y not in t) + " found" # Hope this helps
28th Jul 2021, 2:36 PM
Calvin Thomas
Calvin Thomas - avatar
+ 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.
29th Jul 2021, 3:17 PM
Simon Sauter
Simon Sauter - avatar
+ 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.
29th Jul 2021, 4:46 PM
Simon Sauter
Simon Sauter - avatar
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.
29th Jul 2021, 4:41 PM
Simon Sauter
Simon Sauter - avatar