None shown in output
Terry Reedy
tjreedy at udel.edu
Fri Jun 22 00:33:37 EDT 2012
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
More information about the Python-list
mailing list