Can anyone figure it his out? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

Can anyone figure it his out?

The search() function should return "Word found" if the word is present in the text, or "Word not found", if it’s not. text = input() word = input() x = text.split(' ') def search(x, word): for a in x: if a == word: return "Word found" else: return "Word not found" print(search(x, word)) #it always prints word not found

30th Jun 2022, 3:52 PM
Tshegofatso Sekgothe
4 Respuestas
+ 1
Tshegofatso Sekgothe Because you are returning value so after first iteration value be return. In this case either "Word found" will be print or "Word not found" will be print. x == word #false then else part will be return and loop will be break here so next iteration will not work. Program will be immediately terminate. So don't use loop just do this: if word in x: return "Word found" else return "Word not found"
30th Jun 2022, 4:09 PM
A͢J
A͢J - avatar
+ 3
HINT: Inside the 'for loop', if the first a in text is not the word, the return 'Word not found' in the else block will execute since the if condition is False, and iteration will stop. x = ["This", "is", "awesome"] word = "awesome" 1st iteration: 🔹 a = "This" 🔹 "This" == "awesome" is False 🔹 return 'Word not found' 🔸 return keyword ends the function 🔸 iteration stopped TRY: Instead of using else inside the for loop, only return the 'Word not found' after the for loop.
30th Jun 2022, 4:06 PM
noteve
noteve - avatar
30th Jun 2022, 4:11 PM
A͢J
A͢J - avatar
+ 1
text = input() word = input() if word in text: return "Word found" ......
30th Jun 2022, 4:06 PM
Slick
Slick - avatar