+ 1

How can I search any word in any text and print a message that says word was found or not? Python

That's what i've done but this programme just can find 2 words in text def search (anytext, anyword): for i in anytext: if i == " ": index = list(anytext).index[" "] word1= list(anytext)[index:] word2= list(anytext)[(index + 1):] if word1 == list(word): print ("Word found") elif word2 == list(word): print ("Word found") else: print ("Word not found") text = input() word = input() print(search(text, word))

12th Sep 2021, 2:30 PM
Матвей Стаселько
Матвей Стаселько - avatar
9 Réponses
+ 5
this is the complete task description: 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 ▪︎so you can cut down your code to: def search(text_, word_): if word_ in text_: return "..." else: return"..." text = input() word = input() print(search(text, word))
12th Sep 2021, 2:52 PM
Lothar
Lothar - avatar
+ 4
Pranto , there is a real bunch of issues in your code, that could easily be found by running / testing the code. list(x) creates this list: ['h', 'o', 'l', 'a', ' ', 'a', 'b', 'c', ' '], so the word can not be found in the string. you can use: if "abc" in X: ... your code with some comments: X = “hola abc “ # 2 x wrong quotes, has to be " " if “abc” in list(x): # wrong spelling ox 'x', is declared as "X" || list(x) creates this: ['h', 'o', 'l', 'a', ' ', 'a', 'b', 'c', ' '] print(“found”) # 2 x wrong quotes else: pass #.............. #You can use (in) to search a value.
12th Sep 2021, 4:45 PM
Lothar
Lothar - avatar
+ 3
Матвей Стаселько , are you talking about the python tutorial for beginner, lesson number 45 (search engine)?
12th Sep 2021, 2:48 PM
Lothar
Lothar - avatar
+ 1
You could just check if word in text: # do something If you want to check several words, you can do something like for word in words: if word in text: # do something (words would be a list of words)
12th Sep 2021, 2:46 PM
Lisa
Lisa - avatar
+ 1
Lorhar, yes!
12th Sep 2021, 2:49 PM
Матвей Стаселько
Матвей Стаселько - avatar
+ 1
Yes, but i still can't do it :)
12th Sep 2021, 2:55 PM
Матвей Стаселько
Матвей Стаселько - avatar
+ 1
Oh, thanks! I thought it would be harder but it is really simple
12th Sep 2021, 2:59 PM
Матвей Стаселько
Матвей Стаселько - avatar
+ 1
Lothar thanks man i made a mistake. I am correcting.
12th Sep 2021, 4:49 PM
Iftekhar Ahmed Pranto
Iftekhar Ahmed Pranto - avatar
0
x = “hola abc “ if “abc” in list(x): print(“found”) else: .............. You can use (in) to search a value.
12th Sep 2021, 3:53 PM
Iftekhar Ahmed Pranto
Iftekhar Ahmed Pranto - avatar