Search Engine | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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😅)

26th Apr 2021, 6:37 PM
Elena Vitsiou
Elena Vitsiou - avatar
7 Answers
+ 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.
26th Apr 2021, 6:46 PM
you are smart. you are brave.
you are smart. you are brave. - avatar
+ 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 😉
26th Apr 2021, 6:40 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 3
This should work... text = input() word = input() def search(): if word in text: return "Word found" else: return "Word not found" print(search())
9th Aug 2021, 9:00 AM
Samiur Rahman Samin
+ 2
Thank you all!
26th Apr 2021, 6:54 PM
Elena Vitsiou
Elena Vitsiou - avatar
0
Change the last line by removing the print statement. That's why you are getting None.
26th Apr 2021, 6:49 PM
Jerry Hobby
Jerry Hobby - avatar
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? 😲
26th Apr 2021, 6:51 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
text = input() word = input() def search(text, word): if word in text: print("Word found") else: print("Word not found") search(text, word) 🤟
28th Jun 2022, 10:23 PM
Oğuz