Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: None shown in output

30 views
Skip to first unread message

Michael Hrivnak

unread,
Jun 22, 2012, 12:21:49 AM6/22/12
to Xander Solis, pytho...@python.org
The last three lines print the return value from the "get_numbers"
function, which isn't returning anything. In python, the default
return value is None, and that's why you're seeing it.

Michael

On Thu, Jun 21, 2012 at 11:42 PM, Xander Solis <xrs...@gmail.com> wrote:
> Hello Python list,
>
> Noob here with a newbie question. I'm reading and working on the exercise of
> the book, Learn Python the Hard way 2.0. When I use this code, I get "None"
> on the output. My question is why does this happen?
>
> def get_numbers(first_num, second_num, operator):
>
>     if operator == 'add':
>         print first_num + second_num
>     elif operator == 'minus':
>         print first_num - second_num
>     elif operator == 'divide':
>         print first_num / second_num
>     elif operator == 'multiply':
>         print first_num * second_num
>
> print "%r" % (get_numbers(1, 2, 'minus'))
> print "%r" % (get_numbers(1+3, 2+9, 'add'))
> print "%r" % (get_numbers(10, 2, 'divide'))
>
> Output:
>
> C:\code\python>ex19.py
> -1
> None
> 15
> None
> 5
> None
> 7.5
>
> --
>
> Thanks in advance for your help.
>
> Regards,
>
> Xander
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Benjamin Kaplan

unread,
Jun 22, 2012, 12:24:26 AM6/22/12
to pytho...@python.org
printing something just writes the value to th

Andrew Berg

unread,
Jun 22, 2012, 12:23:59 AM6/22/12
to comp.lang.python
On 6/21/2012 10:42 PM, Xander Solis wrote:
> Hello Python list,
>
> Noob here with a newbie question. I'm reading and working on the
> exercise of the book, Learn Python the Hard way 2.0. When I use this
> code, I get "None" on the output. My question is why does this happen?
Your function prints the number and then returns None (you have no
return statement in the function to change this) to the print statement.
If you want to have the function return a value, use the return
statement instead of print inside the function:

def func(some_value):
return some_value
x = func(5)
print x

will print 5.

--
CPython 3.3.0a4 | Windows NT 6.1.7601.17803

Benjamin Kaplan

unread,
Jun 22, 2012, 12:25:48 AM6/22/12
to pytho...@python.org
damn

On Thu, Jun 21, 2012 at 9:24 PM, Benjamin Kaplan
<benjami...@case.edu> wrote:
> On Thu, Jun 21, 2012 at 8:42 PM, Xander Solis <xrs...@gmail.com> wrote:
>> Hello Python list,
>>
>> Noob here with a newbie question. I'm reading and working on the exercise of
>> the book, Learn Python the Hard way 2.0. When I use this code, I get "None"
>> on the output. My question is why does this happen?
>>
I hate when I accidentally hit send early. Anyway, Michael already
gave you the reason- print and return two different things.

Dan Sommers

unread,
Jun 22, 2012, 12:15:25 AM6/22/12
to pytho...@python.org
On Fri, 22 Jun 2012 11:42:28 +0800
Xander Solis <xrs...@gmail.com> wrote:

> Noob here with a newbie question. I'm reading and working on the
> exercise of the book, Learn Python the Hard way 2.0. When I use this
> code, I get "None" on the output. My question is why does this happen?
>
> def get_numbers(first_num, second_num, operator):
>
> if operator == 'add':
> print first_num + second_num
> elif operator == 'minus':
> print first_num - second_num
> elif operator == 'divide':
> print first_num / second_num
> elif operator == 'multiply':
> print first_num * second_num

This function prints a result, but doesn't return anything.

> print "%r" % (get_numbers(1, 2, 'minus'))
> print "%r" % (get_numbers(1+3, 2+9, 'add'))
> print "%r" % (get_numbers(10, 2, 'divide'))

This:

(get_numbers(1, 2, 'minus'))

calls get_numbers and evaluates to None (because get_numbers doesn't
return anything).

Then this:

"%r" % (get_numbers(1, 2, 'minus'))

formats that None into the string "None."

Finally, this:

print "%r" % (get_numbers(1, 2, 'minus'))

prints that string.

> Output:
>
> C:\code\python>ex19.py
> -1

This "-1" comes from get_numbers.

> None

This "None" comes from the print statement that called get_numbers.

Contrast your code with this snippet:

def add(x, y):
return x + y

print "%r" % (add(3, 2))

HTH,
Dan

--
Μὴ μοῦ τοὺς κύκλους τάραττε -- Αρχιμηδησ
Do not disturb my circles. -- Archimedes

Dan Sommers, http://www.tombstonezero.net/dan

Terry Reedy

unread,
Jun 22, 2012, 12:33:37 AM6/22/12
to pytho...@python.org
On 6/21/2012 11:42 PM, Xander Solis wrote:
> Hello Python list,
>
> Noob here with a newbie question. I'm reading and working on the
> exercise of the book, Learn Python the Hard way 2.0. When I use this
> code, I get "None" on the output. My question is why does this happen?

None is the default return value for python-coded functions and python
prints the value of expressions in interactive mode. Hmmm. But you seem
to be running in batch mode. But where is 7.5 from. You must be doing
more than shown.

Regardless, in my opinion, instead of printing, the function should
return the computed value. You can then print the returned value. Real
software should generally work that way. If nothing else, so you can
automate tests instead of checking screen output.

> def get_numbers(first_num, second_num, operator):
>
> if operator == 'add':
> print first_num + second_num
> elif operator == 'minus':
> print first_num - second_num
> elif operator == 'divide':
> print first_num / second_num
> elif operator == 'multiply':
> print first_num * second_num
>
> print "%r" % (get_numbers(1, 2, 'minus'))
> print "%r" % (get_numbers(1+3, 2+9, 'add'))
> print "%r" % (get_numbers(10, 2, 'divide'))
>
> Output:
>
> C:\code\python>ex19.py
> -1
> None
> 15
> None
> 5
> None
> 7.5

--
Terry Jan Reedy



0 new messages