Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
None shown in output
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Michael Hrivnak  
View profile  
 More options Jun 22 2012, 12:21 am
Newsgroups: comp.lang.python
From: Michael Hrivnak <mhriv...@hrivnak.org>
Date: Fri, 22 Jun 2012 00:21:49 -0400
Local: Fri, Jun 22 2012 12:21 am
Subject: Re: None shown in output
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benjamin Kaplan  
View profile  
 More options Jun 22 2012, 12:24 am
Newsgroups: comp.lang.python
From: Benjamin Kaplan <benjamin.kap...@case.edu>
Date: Thu, 21 Jun 2012 21:24:26 -0700
Local: Fri, Jun 22 2012 12:24 am
Subject: Re: None shown in output

printing something just writes the value to th

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Berg  
View profile  
 More options Jun 22 2012, 12:23 am
Newsgroups: comp.lang.python
From: Andrew Berg <bahamutzero8...@gmail.com>
Date: Thu, 21 Jun 2012 23:23:59 -0500
Local: Fri, Jun 22 2012 12:23 am
Subject: Re: None shown in output
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benjamin Kaplan  
View profile  
 More options Jun 22 2012, 12:25 am
Newsgroups: comp.lang.python
From: Benjamin Kaplan <benjamin.kap...@case.edu>
Date: Thu, 21 Jun 2012 21:25:48 -0700
Local: Fri, Jun 22 2012 12:25 am
Subject: Re: None shown in output
damn

On Thu, Jun 21, 2012 at 9:24 PM, Benjamin Kaplan

I hate when I accidentally hit send early. Anyway, Michael already
gave you the reason- print and return two different things.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dan Sommers  
View profile  
 More options Jun 22 2012, 12:15 am
Newsgroups: comp.lang.python
From: Dan Sommers <d...@tombstonezero.net>
Date: Thu, 21 Jun 2012 21:15:25 -0700
Local: Fri, Jun 22 2012 12:15 am
Subject: Re: None shown in output
On Fri, 22 Jun 2012 11:42:28 +0800

Xander Solis <xrso...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Jun 22 2012, 12:33 am
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Fri, 22 Jun 2012 00:33:37 -0400
Local: Fri, Jun 22 2012 12:33 am
Subject: Re: None shown in output
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.

--
Terry Jan Reedy

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »