search engine (Works but how? and how else? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

search engine (Works but how? and how else?

I solved the question but I do not know how I did it :D I also would like to know how else this problem can be solved. #Problem The given code takes a text and a word as input and passes them to a function called search(). """ text = input() word = input() print(search(text, word)) """ 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 #My code below text = input() word = input() def search(text,word): if word in text: print("Word found") else: print("Word not found") return none print(search(text,word))

10th Oct 2023, 2:18 PM
Nadir Pamuk
Nadir Pamuk - avatar
16 ответов
+ 4
with all the discussions about functions and how best to use them, another topic may have been overlooked. we are supposed to check whether a text contains a *certain word*. example data: sentence => 'this is awesome' search_for => 'awesome' you can simply use this: ... if search_for in sentence: ... for this example data: sentence => 'christmas will come' search_for => 'is' we cannot use the suggested code above because the word *is* does not exist in the sentence as an *independent* word. It is may be probably included as a substring only. if we use the same code as above. the code will find the substring and confirm that the word was found. (which is not correct) for this reason, i suggest a modified code: text = 'christmas will come soon' word = 'is' if word in text.split(): print("Word found" ) else: print("Word not found") this will create a correct result in any case.
10th Oct 2023, 7:05 PM
Lothar
Lothar - avatar
+ 1
Nadir Pamuk, I see you passed the very last exercise of Intro to Python and get the certificate, congratulations. I think you deserve to take a look on an alternative code, along with explanation and other hints. https://code.sololearn.com/clk5s3iyKYLK/?ref=app
10th Oct 2023, 3:00 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Solo, my bad. I intend to say "it is not common" but i missed the "not". There is no need to showcase how you can write complex code. I did say not recommend to include print() in user defined function (here I also missed "user defined"), but in code bit I also said "unless it is REALLY". No one said it is forbidden.
10th Oct 2023, 5:45 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Solo, try this code without 'pass' and 'None'. def my_func(): x = 1 y = 2 z = x + y print(my_func())
10th Oct 2023, 5:53 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Solo, my understanding of "not recommend" is not the same as "forbidden". With "not recommend", we need extra cautious and try to prevent foreseeable mistake or consequence if the action is really needed. Just like smoking is not recommended but not forbidden for adults. And I didn't take it to heart, no offense received.
10th Oct 2023, 6:26 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Solo, I started to learn python just over a year, and I learned this mistake in a hard way. Lothar, you are absolutely right! I didn't read the description carefully, it said "word", not "substring".
10th Oct 2023, 6:50 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Lothar, yes, I did not read the assignment, I proceeded only from the provided code, especially since the author of the question indicated that he passed the test.
10th Oct 2023, 7:15 PM
Solo
Solo - avatar
0
Nadir Pamuk, your function does not require the "return" operator, since it already prints the result. Moreover, there is no such operator as "none". For the same reason, there is no need to print a call to the "search()" function, since it turns out to be essentially print(print()) and since your function does not return anything, "None" is printed. And so is your code with corrections: def search (text,word): if word in text: print("Word found") else: print("Word not found") search(text,word) This function can be shortened by writing the condition in ternary form, but you probably haven't gone through this yet: def search(t,w): print("Word found" if w in t else "Word not found") The same can be done with the Wong Hei Ming code: def search(t,w): return"Word found"if w in t else "Word not found" And since we have come to a function consisting of a single string, then it can be replaced with a lambda function: search=lambda t,w:"Word found"if w in t else"Word not found"
10th Oct 2023, 3:39 PM
Solo
Solo - avatar
0
Solo, it is uncommon a function calling another function. Consider this: print(func_A(func_B(arg_1, arg_2))) While a function include print() is valid, it is not recommended. It is because function ALWAYS return a value, if no value is assigned it return None, a NoneType object. If you want a function to print something, you can return a string and pass it to print().
10th Oct 2023, 3:58 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
Wong Hei Ming, what makes you think that functions always return values? Here is another example of solving this problem using a function that returns nothing: text = "This is awesome" word = "awesome" out = "Word not found" def search(text, word): if word in text: global out out = "Word found" search(text, word) print(out)
10th Oct 2023, 5:13 PM
Solo
Solo - avatar
0
Solo, try the code below and see for yourself. def func_test(): pass print(func_test())
10th Oct 2023, 5:19 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
Wong Hei Ming, and what does it mean that it is not recommended to use a function in a function? Who is not recommended, a teacher who does not know how to read the complex structure of the code of his students? It all depends on the specific function and the task that we face. The structure of the code just happens to be so complex that it is impossible to do without it. Let's return to the reduction of this code: search = lambda t,w: "Word found" if w in t else "Word not found" print(search(input(), input()) Or: print("Word found" if input() in input() else "Word not found") What prevents us from using a function within a function here?
10th Oct 2023, 5:29 PM
Solo
Solo - avatar
0
Wong Hei Ming: "Solo, try the code below and see for yourself. def func_test(): pass print(func_test())" Well, since func_test() returns nothing, then the print function prints "None"...😎 You reread what is "None", "pass" and how the print() function works.
10th Oct 2023, 5:48 PM
Solo
Solo - avatar
0
Wong Hei Ming: "Solo, my bad. I intend to say "it is not common" but i missed the "not". There is no need to showcase how you can write complex code. I did say not recommend to include print() in user defined function (here I also missed "user defined"), but in code bit I also said "unless it is REALLY". No one said it is forbidden." I completely agree with you on this, but only on the condition that this function will need to be used many times without the need for printing. Now do you understand why it is not recommended? Because the function becomes multifunctional, but if the function only requires the output of the result, then there is no need to use additional printing of this function. And don't take it to heart that I challenge your opinion, in fact I object not to you, but to those teachers who give only superficial information often without understanding the essence. When a teacher tells you that it is not recommended to do something, you should always ask a simple question "why?"...😎
10th Oct 2023, 6:09 PM
Solo
Solo - avatar
0
Wong Hei Ming: "Solo, try this code without 'pass' and 'None'. def my_func(): x = 1 y = 2 z = x + y print(my_func())" Oh my God, I'm terrified, you're absolutely right, all functions in Python return "None" by default. I forgot everything, I need to sit down again for textbooks. The fact is that I haven't written in Python for a long time, I need to double-check all the codes that I've written here...😭 Thank you so much for your patience, you made me work on myself...🖐️😎 P.S: "I wouldn't have thought of printing such a function without you...😎"
10th Oct 2023, 6:30 PM
Solo
Solo - avatar
0
Wong Hei Ming: "Solo, my understanding of "not recommend" is not the same as "forbidden". With "not recommend", we need extra cautious and try to prevent foreseeable mistake or consequence if the action is really needed. Just like smoking is not recommended but not forbidden for adults. And I didn't take it to heart, no offense received." I would say that it is not recommended that it does not always mean bad if it is used in the code...😎
10th Oct 2023, 6:55 PM
Solo
Solo - avatar