![]() |
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" |
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 |
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 |
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 |
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. |
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? |
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) |
Re: python not returning true
"John Machin" <sjmachin@lexicon.net> writes:
> What does "pwn" mean? http://en.wikipedia.org/wiki/Pwn |
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 |
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.