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))
14 Respostas
+ 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))
+ 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
+ 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))
+ 3
AJ
What is the use of text.find(word) here?
It will work without it. It was my mistake. Thanks for clarification.
+ 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
+ 2
Kunal š
±ļøšš
What is the use of text.find(word) here?
+ 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"
+ 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 š
+ 1
Gina welcome! But I am not a legend š
š
š
just a poor programmer šŖšŖ
+ 1
Mir Abir Hossain Your too funny š
+ 1
Instead of print inside the function you can use return statement.
And just put to the print function
+ 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 š
+ 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)
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))