Function passed as an argument returns none
Mark Lawrence
breamoreboy at yahoo.co.uk
Wed Oct 1 19:48:44 EDT 2014
On 01/10/2014 23:37, Shiva wrote:
> Hi,
> I am learning Python (version 3.4) strings.I have a function that takes in a
> parameter and prints it out as given below.
>
> def donuts(count):
> if count <= 5:
> print('Number of donuts: ',count)
> else:
> print('Number of donuts: many')
> return
>
> It works fine if I call
> donuts(5)
>
> It returns:
> we have 5 DN (as expected)
It doesn't :) As it takes the first path through the function it will
*print* 'Number of donuts: 5' and then return None as you haven't
specified what your function returns.
>
> However if I do :
>
> test(donuts(4), 'Number of donuts: 4')
>
>
> where test is defined as below:
>
> def test(got, expected):
> print('got: ', got, 'Expected:' ,expected)
> if got == expected:
> prefix = ' OK '
> else:
> prefix = ' X '
> print (('%s got: %s expected: %s') % (prefix, repr(got), repr(expected)))
>
>
> Only 'None' gets passed on to parameter 'got' instead of the expected value
> of 4.
Your expectations are wrong, your function makes no attempt to return
the value you've passed in. I'd (re)read the tutorial again and digest
it, then have another go.
> Any idea why 'None' is getting passed even though calling the donuts(4)
> alone returns the expected value?
What is your expected value? My expected value is None for a value of 4
as you've given a return statement without specifying a value.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Python-list
mailing list