Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'tutorial': 0.03; 'else:': 0.03; 'digest': 0.04; 'from:addr:yahoo.co.uk': 0.04; 'lawrence': 0.09; 'parameter': 0.09; 'prefix': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Function': 0.09; 'wrong,': 0.09; 'python': 0.11; 'def': 0.12; 'language.': 0.14; 'expected)': 0.16; 'expected):': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'specifying': 0.16; 'language': 0.16; 'wrote:': 0.18; 'value.': 0.19; 'print': 0.22; 'header:User- Agent:1': 0.23; 'fine': 0.24; "haven't": 0.24; 'defined': 0.27; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply- To:1': 0.27; 'idea': 0.28; 'function': 0.29; "doesn't": 0.30; 'specified': 0.30; 'statement': 0.30; 'getting': 0.31; 'go.': 0.31; 'prints': 0.31; 'another': 0.32; 'alone': 0.33; "i'd": 0.34; 'test': 0.35; 'in.': 0.36; 'hi,': 0.36; 'expected': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'first': 0.61; "you've": 0.63; 'our': 0.64; 'charset:windows-1252': 0.65; 'below:': 0.68; 'below.': 0.71; 'expectations': 0.74; 'received:2': 0.84; 'returns.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Function passed as an argument returns none Date: Thu, 02 Oct 2014 00:48:44 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-2-100-226-53.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.1.2 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1412207355 news.xs4all.nl 2906 [2001:888:2000:d::a6]:37729 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78571 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