Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > python not returning true

Reply
Thread Tools

python not returning true

 
 
agent-s
Guest
Posts: n/a
 
      02-14-2007
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"

 
Reply With Quote
 
 
 
 
Ben Finney
Guest
Posts: n/a
 
      02-14-2007
"agent-s" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      02-14-2007
On Feb 14, 4:15 pm, "agent-s" <(E-Mail Removed)> 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

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-14-2007
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

 
Reply With Quote
 
agent-s
Guest
Posts: n/a
 
      02-14-2007
On Feb 13, 9:37 pm, "John Machin" <(E-Mail Removed)> wrote:
> On Feb 14, 4:15 pm, "agent-s" <(E-Mail Removed)> 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.

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      02-14-2007
On Feb 14, 5:45 pm, "agent-s" <(E-Mail Removed)> wrote:
> On Feb 13, 9:37 pm, "John Machin" <(E-Mail Removed)> wrote:
>
>
>
> > On Feb 14, 4:15 pm, "agent-s" <(E-Mail Removed)> 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?

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      02-14-2007

"John Machin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...

| On Feb 14, 5:45 pm, "agent-s" <(E-Mail Removed)> 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)



 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      02-14-2007
"John Machin" <(E-Mail Removed)> writes:
> What does "pwn" mean?


http://en.wikipedia.org/wiki/Pwn
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      02-14-2007
On Feb 14, 7:02 pm, "Terry Reedy" <(E-Mail Removed)> wrote:
> "John Machin" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) ups.com...
>
> | On Feb 14, 5:45 pm, "agent-s" <(E-Mail Removed)> 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

 
Reply With Quote
 
Michael Bentley
Guest
Posts: n/a
 
      02-14-2007

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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[False,True] and [True,True] --> [True, True]????? bdb112 Python 45 04-29-2009 02:35 AM
irb: 1 & 1 => 1, not true huh? I thought 1 & 1 => true Peter Alvin Ruby 4 09-29-2008 08:08 AM
RE: Page.User.IsInRole True for AD group in VS.NET not true on samemachine once published wildman@noclient.net ASP .Net 0 03-10-2008 08:54 PM
"require" in unit test not returning true or false Jesper Rønn-Jensen Ruby 5 07-23-2007 08:39 PM
C and C++ are interoperable languages? True or Not True? Chip C++ 6 01-08-2005 11:10 PM



Advertisments