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
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"
+ 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.
+ 3
Tshegofatso Sekgothe
And delete this duplicate question
https://www.sololearn.com/Discuss/3053699/?ref=app
+ 1
text = input()
word = input()
if word in text:
return "Word found"
......