+ 1
HELP In my python code.
Here's the code : text = input() word = input() def search(word,text): if (text) in (word): print("Word found") else: print("Word not found") print(search(text,word)) Problem - it is giving the intended output but it is also printing "None" can't find the bug .
4 Réponses
+ 8
Your search() function does not return anything. It prints the search status on its own. So when the line ...
print( search( text, word ) )
Was executed, it prints 'None' because search() does not give anything back, for print() to write to the console screen.
Just call the search() function, don't put the call inside print()
search( text, word ) # no print()
+ 4
Rishi Majumdar
Ipang has already given you a great answer, but I wish to elaborate further because I had this exact problem during my learning process.
When you place print(something) inside a def, then you only call the function as per Ipang's instruction.
The advantage of this is that your function can output multiple results, (depending on the internal func code) with the single function call.
If your def has a return result, then you need to print(your function) if you wish to see the result.
Advantages of returning a result is that the return will stop any further process of the function, and you can use the return result in further coding, even if you haven't printed it.
This flexibility allows you to build little process machines within your main code.
It's really cool!
+ 2
ok so you printed a function that doesn't return any value so you printed None
None is the default return of a function that doesn't return any value
you can just call the function search
cause it has a print already
like
search("this is a word","text")
or you can return a value example
def search(word,text):
if text in word:
return f"{text} was found in {word}"
then you can print the returned value
print(search("hello","he"))
expected output would be
he was found in hello
+ 2
It's also worth pointing out that while sololearn accepts codes that use print statements inside the function, that is not a correct way to solve the task. The task is to write a function that returns a string and then print the returned string outside the function.