Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   python not returning true (http://www.velocityreviews.com/forums/t399203-python-not-returning-true.html)

agent-s 02-14-2007 05:15 AM

python not returning true
 
I have a function, generally described as so:

def function(args):
if condition:
if condition2:
function(args+1)
elif condition3:
print "text"
return True
else:
return False

which is used in:

if function(args):
print "ok"


so here basically "text" will print out when condition3 is true but it
will not print out "ok" when condition3 is true. When it's true it
should print out borth "text" and "ok"


Ben Finney 02-14-2007 05:36 AM

Re: python not returning true
 
"agent-s" <shanekwon@gmail.com> writes:

> I have a function, generally described as so:
>
> def function(args):
> if condition:
> if condition2:
> function(args+1)
> elif condition3:
> print "text"
> return True
> else:
> return False


You've simplified this, presumably to make the code more
clear. Unfortunately what remains isn't executable, so we can't see
the behaviour that confuses you.

Please write a minimal example that demonstrates the behaviour you
want explained.

--
\ "I doubt, therefore I might be." -- Anonymous |
`\ |
_o__) |
Ben Finney


John Machin 02-14-2007 05:37 AM

Re: python not returning true
 
On Feb 14, 4:15 pm, "agent-s" <shanek...@gmail.com> wrote:
> I have a function, generally described as so:
>


> def function(args):
> if condition:
> if condition2:
> function(args+1)

return None
> elif condition3:
> print "text"
> return True
> else:
> return False

else:
return None

There are two cases, indicated above, where you don't explicitly do a
"return", so you fall off the end of the function, and Python returns
None.

Then when the function's caller tests the returned value, None is
treated as logically false.

> which is used in:
>
> if function(args):
> print "ok"
>
> so here basically "text" will print out when condition3 is true but it
> will not print out "ok" when condition3 is true. When it's true it
> should print out borth "text" and "ok"


In the second last sentence, it is difficult to determine what you
think is expected behaviour and what you say is the actual behaviour.
In the last sentence, what does the first "it" refer to?

If the knowledge about returning None doesn't help you, try some
standard(??) techniques like inserting print statements or debugger
break-points.

HTH,
John


Steven D'Aprano 02-14-2007 05:37 AM

Re: python not returning true
 
On Tue, 13 Feb 2007 21:15:19 -0800, agent-s wrote:

> I have a function, generally described as so:

[snip function]
> which is used in:
>
> if function(args):
> print "ok"
>
> so here basically "text" will print out when condition3 is true but it
> will not print out "ok" when condition3 is true. When it's true it
> should print out borth "text" and "ok"


Thank you for sharing. Do you have an actual question?




--
Steven D'Aprano


agent-s 02-14-2007 06:45 AM

Re: python not returning true
 
On Feb 13, 9:37 pm, "John Machin" <sjmac...@lexicon.net> wrote:
> On Feb 14, 4:15 pm, "agent-s" <shanek...@gmail.com> wrote:
>
> > I have a function, generally described as so:

>
> > def function(args):
> > if condition:
> > if condition2:
> > function(args+1)

>
> return None> elif condition3:
> > print "text"
> > return True
> > else:
> > return False

>
> else:
> return None
>
> There are two cases, indicated above, where you don't explicitly do a
> "return", so you fall off the end of the function, and Python returns
> None.
>
> Then when the function's caller tests the returned value, None is
> treated as logically false.
>
> > which is used in:

>
> > if function(args):
> > print "ok"

>
> > so here basically "text" will print out when condition3 is true but it
> > will not print out "ok" when condition3 is true. When it's true it
> > should print out borth "text" and "ok"

>
> In the second last sentence, it is difficult to determine what you
> think is expected behaviour and what you say is the actual behaviour.
> In the last sentence, what does the first "it" refer to?
>
> If the knowledge about returning None doesn't help you, try some
> standard(??) techniques like inserting print statements or debugger
> break-points.
>
> HTH,
> John


Thanks! That was exactly what it was. I solved it by using "return
function(args+1)" instead of simply "function(args+1)."

btw Steven you are so witty I hope to one day pwn noobs on newsgroups
too.


John Machin 02-14-2007 07:15 AM

Re: python not returning true
 
On Feb 14, 5:45 pm, "agent-s" <shanek...@gmail.com> wrote:
> On Feb 13, 9:37 pm, "John Machin" <sjmac...@lexicon.net> wrote:
>
>
>
> > On Feb 14, 4:15 pm, "agent-s" <shanek...@gmail.com> wrote:

>
> > > I have a function, generally described as so:

>
> > > def function(args):
> > > if condition:
> > > if condition2:
> > > function(args+1)

>
> > return None> elif condition3:
> > > print "text"
> > > return True
> > > else:
> > > return False

>
> > else:
> > return None

>
> > There are two cases, indicated above, where you don't explicitly do a
> > "return", so you fall off the end of the function, and Python returns
> > None.

>
> > Then when the function's caller tests the returned value, None is
> > treated as logically false.

>
> > > which is used in:

>
> > > if function(args):
> > > print "ok"

>
> > > so here basically "text" will print out when condition3 is true but it
> > > will not print out "ok" when condition3 is true. When it's true it
> > > should print out borth "text" and "ok"

>
> > In the second last sentence, it is difficult to determine what you
> > think is expected behaviour and what you say is the actual behaviour.
> > In the last sentence, what does the first "it" refer to?

>
> > If the knowledge about returning None doesn't help you, try some
> > standard(??) techniques like inserting print statements or debugger
> > break-points.

>
> > HTH,
> > John

>
> Thanks! That was exactly what it was. I solved it by using "return
> function(args+1)" instead of simply "function(args+1)."


That takes care of only 1 of the two cases of returning None instead
of True/False.

>
> btw Steven you are so witty I hope to one day pwn noobs on newsgroups
> too.


Wit has nothing to do with it. The fact that you are a Python noob is
also irrelevant. Your problem statement was unintelligible, as is your
response. What does "pwn" mean?


Terry Reedy 02-14-2007 08:02 AM

Re: python not returning true
 

"John Machin" <sjmachin@lexicon.net> wrote in message
news:1171437351.206085.236450@q2g2000cwa.googlegro ups.com...

| On Feb 14, 5:45 pm, "agent-s" <shanek...@gmail.com> wrote:
| > btw Steven you are so witty I hope to one day pwn noobs on newsgroups
| > too.

Sorry, but you are 'pwning' yourself here ;-)

| Wit has nothing to do with it. The fact that you are a Python noob is
| also irrelevant. Your problem statement was unintelligible, as is your
| response. What does "pwn" mean?

I believe that it is a misspelling of 'own' used by pvp (person versus
person, as opposed to person versus monster) gamers to demonstrate their
in-ness. But perhaps agent-s can enlightenment us further.

Terry Jan Reedy (occasional, non-elite gamer)




Paul Rubin 02-14-2007 08:07 AM

Re: python not returning true
 
"John Machin" <sjmachin@lexicon.net> writes:
> What does "pwn" mean?


http://en.wikipedia.org/wiki/Pwn

John Machin 02-14-2007 09:08 AM

Re: python not returning true
 
On Feb 14, 7:02 pm, "Terry Reedy" <tjre...@udel.edu> wrote:
> "John Machin" <sjmac...@lexicon.net> wrote in message
>
> news:1171437351.206085.236450@q2g2000cwa.googlegro ups.com...
>
> | On Feb 14, 5:45 pm, "agent-s" <shanek...@gmail.com> wrote:
> | > btw Steven you are so witty I hope to one day pwn noobs on newsgroups
> | > too.
>
> Sorry, but you are 'pwning' yourself here ;-)


And the referent of "you" would be .....?

>
> | Wit has nothing to do with it. The fact that you are a Python noob is
> | also irrelevant. Your problem statement was unintelligible, as is your
> | response. What does "pwn" mean?
>
> I believe that it is a misspelling of 'own' used by pvp (person versus
> person, as opposed to person versus monster) gamers to demonstrate their
> in-ness. But perhaps agent-s can enlightenment us further.


So "enlightenment" has been verbed, has it? I didn't realise that the
language had been transitioned so far :-)

Cheers,
John


Michael Bentley 02-14-2007 09:20 AM

Re: python not returning true
 

On Feb 14, 2007, at 3:08 AM, John Machin wrote:

> So "enlightenment" has been verbed, has it? I didn't realise that the
> language had been transitioned so far :-)


*ALL* nouns may be verbed ;-)

-michael
---
# Something just doesn't seem right in those
# "Every kiss begins with 'K'" commercials.

>>> 'Every Kiss'.startswith('K')

False






All times are GMT. The time now is 08:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57