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
7/28/2021 4:31:25 AMJayashree Das
18 AnswersNew Answer
What have you tried so far?
def seacrh(text,word) : if word in text : return 'Word found' else: return 'Word not found' text = input() word = input() print(seacrh(text,word))
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.
It's a bug. Try again later https://www.sololearn.com/discuss/2845292/?ref=app
Jayashree Das Working fine. No issue.
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.
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)
Simon Sauter check the output man
Simon Sauter ok
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".
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)
Jayashree Das How about this one: search = lambda x, y: "Word" + " not" * (y not in t) + " found" # Hope this helps
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.
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.
Yes I tried but the ans. Is error Plz tell what is it's correct ans.
def seacrh(text,word) : if word in text : print ('Word found') else: print('Word not found') text = input() word = input() seacrh(text,word)
Line 5 Invalid syntax this is the essue
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.