+ 5
Search Engine
Hey guys, can anyone tell me why it prints None after the answer in my code? def search(x, y): x = x.split() if y in x: print("Word found") else: print("Word not found") text = input() word = input() print(search(text, word)) I am new here and I am trying to learn the basics (meaning: I am failing to learn them😅)
7 odpowiedzi
+ 4
Ярослав Вернигора(Yaroslav Vernigora) is correct that with the print statement within the function is sufficient.
the reason you are seeing None is most likely because you have no return line within the function. for you to get the result of a function you are calling you need to end the function with a return line with the results you want returned. an example would be like:
def function(x):
answer = x*2
return answer
result = function(2)
print(result) #4
if i didnt have the return line, it would print None, because i didnt return anything when i called the function. i hope that makes sense.
+ 5
Hi! you don't need to call the print function and insert your function there. your function is already self-sufficient. just call it at the end of the program 😉
+ 3
This should work...
text = input()
word = input()
def search():
if word in text:
return "Word found"
else:
return "Word not found"
print(search())
+ 2
Thank you all!
0
Change the last line by removing the print statement. That's why you are getting None.
0
I think that in our case, the return is not necessary, because we do not output any values, we just print two phrases, depending on the condition.
Wow! Is that a lot of words you just typed yourself? 😲
0
text = input()
word = input()
def search(text, word):
if word in text:
print("Word found")
else:
print("Word not found")
search(text, word)
🤟