why is there a extra none in output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

why is there a extra none in output

text = input() word = input() def search(text,word): if word in text: print("Word found") return else: print("Word not found") return print(search(text, word))

24th Aug 2021, 3:19 AM
Sourav O
Sourav O - avatar
6 Answers
+ 10
Because you are printing a function that contains a print() function. The output of printing print() is None. Also, the "return" statements in your function don't add anything to the code. You could just leave them out. If you are going call your function by putting it in a print() function, then don't have your function print the word results, just return them. E.g. if word in text: return "Word found" else: return "Word not found" then print(search(text, word)) should work. Alternatively, if your function says if word in text: print("Word found") else: print("Word not found") Then simply call the function by saying search(text, word) And that should also work.
24th Aug 2021, 3:30 AM
David Ashton
David Ashton - avatar
+ 4
You should just be returning the strings in your if statements. def search(text, word): if word in text: return "Word found" else: return "Word not found" When a function does not explicitly return a value, None is returned implicitly. This means when it lacks any return statement, or reaches a return statement without a value, that None is returned from the function. Here you are printing the string in the function, then returning None from the function and printing it.
24th Aug 2021, 3:36 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
You can try an experiment. Run this code: print(print("Hi")) The output is: Hi None The inner print function sends "Hi" to the console but doesn't return anything, i.e returns None, which the outer function sends to the console.
24th Aug 2021, 5:42 AM
David Ashton
David Ashton - avatar
+ 3
JUMP_LINK__&&__Python__&&__JUMP_LINK It's simple, if your function doens't have any return value then the function will return None.
24th Aug 2021, 4:57 AM
A͢J
A͢J - avatar
+ 2
JUMP_LINK__&&__Python__&&__JUMP_LINK, That's because function print() returns nothing (Well, it actually returns None). If you run: print("Hi everyone") The print() function is not returning anything else than None. When you try to print the value returned by print: print(print("Hi everyone")) The outer print() prints the argument passed to it, in this case, "None" (value returned by the inner print() ) Process: print("Hi") -> Prints "Hi\n", returns None | print(print("Hi")) -> prints None (from inner print())
24th Aug 2021, 6:28 AM
Hacker-KR4636
Hacker-KR4636 - avatar
0
The extra `None` in the output is because your `search` function does not have a return statement for the case when the word is found. When a function does not explicitly return a value, Python automatically returns `None` by default. In your code, you are already printing "Word found" or "Word not found" within the `search` function, so there is no need to print the return value of the function in the `print` statement outside the function. By removing the `print` statement around the `search` function call, you can avoid the extra `None` in the output. Here's the updated code: text = input() word = input() def search(text, word): if word in text: print("Word found") else: print("Word not found") search(text, word) With this modification, the code will print either "Word found" or "Word not found" directly from the `search` function without printing the return value, eliminating the extra `None` in the output, want python code snippets for your problem go here: https://coderhax.com/snippets/python
5th Jul 2023, 4:41 AM
Jarvis Silva
Jarvis Silva - avatar