Search Engine in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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))

24th Jan 2022, 6:12 AM
Gina
14 Answers
+ 7
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))
24th Jan 2022, 9:17 AM
A͢J
A͢J - avatar
+ 3
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
24th Jan 2022, 10:24 AM
Gina
+ 3
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))
25th Jan 2022, 2:19 PM
KINGFISHER
KINGFISHER - avatar
+ 3
AJ What is the use of text.find(word) here? It will work without it. It was my mistake. Thanks for clarification.
26th Jan 2022, 2:30 PM
KINGFISHER
KINGFISHER - avatar
+ 2
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
25th Jan 2022, 7:05 AM
Saad Khan
Saad Khan - avatar
+ 2
Kunal 🅱️💖🔐 What is the use of text.find(word) here?
25th Jan 2022, 6:04 PM
A͢J
A͢J - avatar
+ 1
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"
24th Jan 2022, 6:22 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 1
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 🏆
24th Jan 2022, 6:50 AM
Gina
+ 1
Gina welcome! But I am not a legend 😅😅😅 just a poor programmer 😪😪
24th Jan 2022, 6:55 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 1
Mir Abir Hossain Your too funny 😂
24th Jan 2022, 7:02 AM
Gina
+ 1
Instead of print inside the function you can use return statement. And just put to the print function
24th Jan 2022, 8:00 AM
Shadoff
Shadoff - avatar
+ 1
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 😀
25th Jan 2022, 7:40 AM
Gina
+ 1
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)
26th Jan 2022, 5:56 AM
Avery
0
Hi, try this, it works: text = input() word = input() def search(text, word): if word in text: return("Word found") else: return("Word not found") print(search(text, word))
18th Sep 2022, 11:38 AM
David Herceg
David Herceg - avatar