Search Engine in Python
The challenge is to design a search engine, and the code below achieves that, however it prints a second line saying “None” and I can’t get rid of it. I’ve tried everything that I can think of, but I’ve never seen this happen before? Does anyone know how to get rid of it, or where I went wrong? Thanks, Gina def search(text, word): if word in text: print("Word found") else: print("Word not found") text = input("") word = input("") print(search(text, word))
1/24/2022 6:12:41 AMGina
13 AnswersNew Answer
Mir Abir Hossain It is not the issue of double print it's the issue of non return function means if your function doesn't return anything then it will print None. As print function doesn't return anything so if you print this function then output would be None Try this: print(print()) Gina In your case you are not returning anything instead of printing inside function so no need to print function again. If you want to print function then your function should return something. So your function should be like this: def search (text, word): if word in text: return "Word found" else: return "Word not found" print (search (text, word))
A͢J Shadoff Thank you both so much. All comments on here have given me a clear view of two methods. The return function example is awesome, clearly shows how returns work with strings, as I have just learnt the return function, but that was with integers, so I couldnt fully understand with strings, so the example taught me much 😀 Thank you everyone, I have learnt a lot from you all. Cheers, Gina
Gina let check my program for search engine: def search(text, word): text.find(word) if word in text: return 'Word found' else: return 'Word not found' text = input() word = input() print(search(text, word))
AJ What is the use of text.find(word) here? It will work without it. It was my mistake. Thanks for clarification.
Using print inside a function (def funtion_name) , actually results in None sometimes in output, this issue was fixed when i used print(something.filter('None'). Ive created a module for printing text in different colors. the only issue i encountered was None in output, but now I've fixed this issue and soon upload the module on GitHub
Kunal 🅱️💖🔐 What is the use of text.find(word) here?
search(text, word) => print("Word found") So, print(search(text, word)) => print(print("Word found")) When you double print something it outputs None Example--> print(print(5)) output --> 5 None So you need to remove one print. Either you write search(text, word) or inside function you write return "Word found"
Mir Abir Hossain Thankyou so much, it took a few minutes, but then I got it. Just like you said, I took the print function out of the last line of code, and it worked beautifully. Thank you, your a legend 🏆
Gina welcome! But I am not a legend 😅😅😅 just a poor programmer 😪😪
Mir Abir Hossain Your too funny 😂
Instead of print inside the function you can use return statement. And just put to the print function
Saad Khan Thanks for the tip, I’ll make a note of that, for next time i come up with the same occurence. Also, your module that you created sounds awesome. Have a great week 😀
Reading through this, I realize I made mine way too complex. text = input() word = input() def search(x,y): F = text.split(" ") sum = 0 tot = 0 for i in F: if i == word: tot += 1 if sum == tot: print("Word not found") else: print("Word found") search(text, word)