0

hey guys ! I need help with my code it seems as if something is missing I tried using for loops but I couldn't manage

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 text = input() word = input() def search(text,word): if text == word or word == text: return("Word found") else: return("Word not found") print(search(text ,word))

15th Nov 2021, 8:24 AM
siviwe sam-sam
2 Réponses
+ 1
At the current code, you are putting the one word against the entire sentence so that is the reason it doesn't work. I would recommend you think of a way to compare your single word to every word in the sentence given.
15th Nov 2021, 8:30 AM
Ben Eshchar
Ben Eshchar - avatar
0
Your code look like this: word = "python" text = "python is amazing" if "python" == "python is amazing" or "python is amazing" == "python": # do something... You need to check (if word in text) , not to compare 2 strings like you tried. Solution using for loop, will need to loop trought each word, so you need to somehow convert this to list of words (text.split() can do this) and compare each word with given word. This is what using "in" do for us.
15th Nov 2021, 8:57 AM
PanicS
PanicS - avatar