python not returning true

A

agent-s

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"
 
B

Ben Finney

agent-s said:
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.
 
J

John Machin

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
 
S

Steven D'Aprano

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?
 
A

agent-s

return None> elif condition3:

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.




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.
 
J

John Machin

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?
 
T

Terry Reedy

| > 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)
 
J

John Machin

| > 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
 
G

Grant Edwards

| 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.

Mis-spelling things is witty now? Wow. I've been witty all
these years and didn't even know it...
 
D

Dennis Lee Bieber

Michael Bentley said:
# Something just doesn't seem right in those
# "Every kiss begins with 'K'" commercials.
'Every Kiss'.startswith('K') False
kisses = ["kiss", "kiss", "kiss", "kiss", "kiss"]
kisses == [kiss for kiss in kisses
... if kiss.startswith("k")]
True

Happy St. Valentine's Day, everyone.

Technically, it is not the initial "K", but the store name "Kay"

{Does that imply a hick drawl of "Kayiss"?}
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
T

Terry Reedy

| > | > 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 .....?

agent-s, as I though was obvious.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top