None shown in output

Xander Solis xrsolis at gmail.com
Fri Jun 22 00:33:06 EDT 2012


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.com>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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120622/07fcbb57/attachment.html>


More information about the Python-list mailing list