Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra output none while printing an command line argument [duplicate]

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?

like image 704
CuriousMind Avatar asked Oct 17 '10 12:10

CuriousMind


People also ask

Why is there a None in my output?

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.

How do I stop print 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.

Why does None get printed in Python?

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.

How will you print space and stay on the same line remember print () by default goes to next line after printing?

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.


1 Answers

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.

like image 144
Thomas Wouters Avatar answered Oct 05 '22 11:10

Thomas Wouters