+ 1
My code for project 8 python for beginners, why does it output 'none' when I run it
def search(): if word in text: print("Word found") else: print("Word not found") text = input() word = input() print(search()) https://code.sololearn.com/cr7rW0UXIRCt/?ref=app https://code.sololearn.com/cr7rW0UXIRCt/?ref=app
9 odpowiedzi
+ 5
Ackumey Joseph
Finally, your 3rd problem
If you define a print statement inside a def, then it will print out a result WHEN the def is called.
So
def test():
print("tested")
will create the following results when called in the following manner
test() -> "tested"
print(tested()) -> "tested"
-> none
+ 2
You forgot to provide parameters/arguments.
+ 2
Ackumey Joseph
There are a few problems with your code.
The biggest problem is that your def is isolated fron your inputs as you have not provided parameters for you def to work on.
As noted by Simon Sauter
Another problem is that you must define the words in the text, so your word will match.
Otherwise
text = "this test"
word = "is
will return "word found"
Take note of the .split() method which will break a string into a list.
def search(item, phrase):
if item in phrase.split(" "):
print("Word found")
else:
print("Word not found")
text = input()
word = input()
search(word,text)
+ 1
Remove the print inside the def if statements with return and the quote you want
+ 1
When you execute a function it will always return a value. It either returns the the value you tell it to using the "return" keyword or (no return is used) "null" by default.
Now, when you call that function somewhere it will be evaluated and its returned value might be used, in your case you are trying to print it out to the console. as you haven't used "return", null is returned instead.
You can fix your code in 2 ways, either return the string directly instead of printing it inside the function, or just print it inside the function and delete the print() on the outside.
0
Help me
0
def search(word, text):
if word in text:
print("Word found")
else:
print("Word not found")
text = input()
word = input()
search(word=word, text=text)
“””
You have to pass the variables word and text into the search function as parameters. The variables word and text are compared within the function search. But for the function to use them you must pass them to it.
Your code returns none because nothing is passed to the function and nothing is returned.
“””
0
You can get rid of it py a filter() function of python.
print(tested().filter('None'))
or use return if you are printing the function. Hope you understood