Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

None shown in output

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


xrsolis at gmail

Jun 21, 2012, 8:42 PM

Post #1 of 9 (243 views)
Permalink
None shown in output

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


dan at tombstonezero

Jun 21, 2012, 9:15 PM

Post #2 of 9 (240 views)
Permalink
Re: None shown in output [In reply to]

On Fri, 22 Jun 2012 11:42:28 +0800
Xander Solis <xrsolis [at] gmail> 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
--
http://mail.python.org/mailman/listinfo/python-list


mhrivnak at hrivnak

Jun 21, 2012, 9:21 PM

Post #3 of 9 (238 views)
Permalink
Re: None shown in output [In reply to]

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 <xrsolis [at] gmail> 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
>
--
http://mail.python.org/mailman/listinfo/python-list


bahamutzero8825 at gmail

Jun 21, 2012, 9:23 PM

Post #4 of 9 (239 views)
Permalink
Re: None shown in output [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


benjamin.kaplan at case

Jun 21, 2012, 9:24 PM

Post #5 of 9 (240 views)
Permalink
Re: None shown in output [In reply to]

On Thu, Jun 21, 2012 at 8:42 PM, Xander Solis <xrsolis [at] gmail> 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
>

printing something just writes the value to th
--
http://mail.python.org/mailman/listinfo/python-list


benjamin.kaplan at case

Jun 21, 2012, 9:25 PM

Post #6 of 9 (238 views)
Permalink
Re: None shown in output [In reply to]

damn

On Thu, Jun 21, 2012 at 9:24 PM, Benjamin Kaplan
<benjamin.kaplan [at] case> wrote:
> On Thu, Jun 21, 2012 at 8:42 PM, Xander Solis <xrsolis [at] gmail> 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
>>
>
> printing something just writes the value to th

I hate when I accidentally hit send early. Anyway, Michael already
gave you the reason- print and return two different things.
--
http://mail.python.org/mailman/listinfo/python-list


xrsolis at gmail

Jun 21, 2012, 9:33 PM

Post #7 of 9 (238 views)
Permalink
Re: None shown in output [In reply to]

Thanks Andrew and Michael!. That did the trick.

def get_numbers(first_num, second_num, operator):

if operator == 'add':
value = first_num + second_num
elif operator == 'minus':
value = first_num - second_num
elif operator == 'divide':
value = first_num / second_num
elif operator == 'multiply':
value = first_num * second_num

return value

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

output:

-1
15
5

On Fri, Jun 22, 2012 at 12:23 PM, Andrew Berg <bahamutzero8825 [at] gmail>wrote:

> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>


tjreedy at udel

Jun 21, 2012, 9:33 PM

Post #8 of 9 (238 views)
Permalink
Re: None shown in output [In reply to]

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



--
http://mail.python.org/mailman/listinfo/python-list


xrsolis at gmail

Jun 21, 2012, 9:39 PM

Post #9 of 9 (238 views)
Permalink
Re: None shown in output [In reply to]

Thanks for replying still! Appreciate the help.

On Fri, Jun 22, 2012 at 12:25 PM, Benjamin Kaplan
<benjamin.kaplan [at] case>wrote:

> damn
>
> On Thu, Jun 21, 2012 at 9:24 PM, Benjamin Kaplan
> <benjamin.kaplan [at] case> wrote:
> > On Thu, Jun 21, 2012 at 8:42 PM, Xander Solis <xrsolis [at] gmail> 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
> >>
> >
> > printing something just writes the value to th
>
> I hate when I accidentally hit send early. Anyway, Michael already
> gave you the reason- print and return two different things.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.