It's my day 1 of learning python. so it's a noob question for many of you. See the following code:
#!/usr/bin/env python
import sys
def hello(name):
name = name + '!!!!'
print 'hello', name
def main():
print hello(sys.argv[1])
if __name__ == '__main__':
main()
when I run it
$ ./Python-1.py alice
hello alice!!!!
None
Now, I have trouble understanding where this "None"
came from?
Functions often print None when we pass the result of calling a function that doesn't return anything to the print() function. All functions that don't explicitly return a value, return None in Python.
The function call "print(movie_review(9)) is attempting to print the return value. Without a return statement this defaults to none. It can be fixed by adding a return statement on each conditional statement and removing the print.
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.
Modify print() method to print on the same lineThe print method takes an extra parameter end=” “ to keep the pointer on the same line. The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.
Count the number of print
statements in your code. You'll see that you're printing "hello alice!!!"
in the hello
function, and printing the result of the hello
function. Because the hello
function doesn't return a value (which you'd do with the return
statement), it ends up returning the object None
. Your print
inside the main
function ends up printing None
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With