+ 1

Search engine challenge - python beginner

I don't understand the solution for the easy search engine. It was something like: If [string] in [string with multiple words]: Print("found") Else: Print("not found") In which lesson did we learn that we can look for a part of a string using the if-statement? I couldn't find it. I do understand the logic behind the loop, but I don't know when I learned it.

15th Mar 2023, 7:45 AM
Simba Estreich
Simba Estreich - avatar
7 Respuestas
+ 7
Simba Estreich , the *search engine* exercise is the last one in the tutorial. it requires the *in* operator. > this is explained in the *python for beginners* tutorial in the chapter *lists* > the lesson 32.1 *list operations* demonstrates: words = ["spam", "egg", "spam", "sausage"] print("spam" in words) print("egg" in words) print("tomato" in words) and it also says: The *in* operator is also used to determine whether or not a *string* is a *substring* of another string.
15th Mar 2023, 10:57 AM
Lothar
Lothar - avatar
+ 1
#Aim of the program is to check for a word in a given sentence and print found if it's present So the solution will be text = input() word = input() def search(text,word): if word in text: return("Word found") else: return("Word not found") print(search(text,word))
15th Mar 2023, 9:05 AM
Herobrine
Herobrine - avatar
+ 1
Lothar, Thank you so much! I knew I overlooked something!
15th Mar 2023, 11:12 AM
Simba Estreich
Simba Estreich - avatar
0
Please go through introduction of python one more time than you will Find logic behind the loop An about if statement
15th Mar 2023, 9:01 AM
Herobrine
Herobrine - avatar
0
You have to understand the aim Then you can solve it I think you did not understand was the aim of code
15th Mar 2023, 9:07 AM
Herobrine
Herobrine - avatar
0
If I am wrong please correct me
15th Mar 2023, 9:07 AM
Herobrine
Herobrine - avatar
0
Thanks herobrine for your answers. I did understand the goal and I tried with big loops. But the solution (in my post) surprised me, because it was so easy. I expected a "bigger" solution, as it was the final project for the beginner course. But because it was such an easy solution, I wondered where in the course they taught that you can search for a word in a string, only using a if-loop.
15th Mar 2023, 9:12 AM
Simba Estreich
Simba Estreich - avatar