0
It also shows "None" when I run the code whether the word is found or not. How do I do? Please explain me. And thank you
6 Respuestas
+ 7
When you return the value then store in any variable and then print then you don't get 'None' value, like that
result = search(word, text)
+ 4
Your Code:
text = input()
word = input()
def search(word, text):
for i in word:
if word in text:
print("Word found")
return
else:
print("Word not found")
return
print(search(word, text))
I think you should remove for loop it's unnecessary and also return value is not returning anything tyou should pplace it outside thhe loop hthat's why there is 'none' and remove print statement from calling the function as it already includes it so it is unnecessary.
Try this code Insted:
https://code.sololearn.com/cvOQsU926hLo/?ref=app
+ 3
Kyaw Hok well it shows None cause your function returns that value
instead of returning that try:
returning the string inside the print statement
or
just call that function without a print statement
+ 2
"none" issues aside, I feel I have to mention this:
for i in word
if word in text
Idk what you think that does, but it doesn't. That counts through each letter in the string word, and then checks whether the whole string is in text that many times. Except it doesn't, because whether it finds it or not, you return immediately.
But let's suppose you changed it to not return until after the for loop; then it would print either "word found" or "word not found" n times, where n is the length of the string in word, not even depending on how many times word appears in text just if it... still does since last time, I guess?
So please, for my sanity, remove that "for i in word" line; you don't use it, and it would be nonsense if you did.
0
You do not need loop "for", cause operator "in" do it itself.
You got None cause you try print in def search, and do it again when call func print(search(**)).
So your func may looks like this:
text = input()
word = input()
def search(w, t):print("Word found"if w in t else "Word not found")
search(word, text)