python not returning true

Discussion in 'Python' started by agent-s, Feb 14, 2007.

  1. agent-s

    agent-s Guest

    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"
     
    agent-s, Feb 14, 2007
    #1
    1. Advertising

  2. agent-s

    Ben Finney Guest

    "agent-s" <> 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
     
    Ben Finney, Feb 14, 2007
    #2
    1. Advertising

  3. agent-s

    John Machin Guest

    On Feb 14, 4:15 pm, "agent-s" <> 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
     
    John Machin, Feb 14, 2007
    #3
  4. 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
     
    Steven D'Aprano, Feb 14, 2007
    #4
  5. agent-s

    agent-s Guest

    On Feb 13, 9:37 pm, "John Machin" <> wrote:
    > On Feb 14, 4:15 pm, "agent-s" <> 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.
     
    agent-s, Feb 14, 2007
    #5
  6. agent-s

    John Machin Guest

    On Feb 14, 5:45 pm, "agent-s" <> wrote:
    > On Feb 13, 9:37 pm, "John Machin" <> wrote:
    >
    >
    >
    > > On Feb 14, 4:15 pm, "agent-s" <> 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?
     
    John Machin, Feb 14, 2007
    #6
  7. agent-s

    Terry Reedy Guest

    "John Machin" <> wrote in message
    news:...

    | On Feb 14, 5:45 pm, "agent-s" <> 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)
     
    Terry Reedy, Feb 14, 2007
    #7
  8. agent-s

    Paul Rubin Guest

    Paul Rubin, Feb 14, 2007
    #8
  9. agent-s

    John Machin Guest

    On Feb 14, 7:02 pm, "Terry Reedy" <> wrote:
    > "John Machin" <> wrote in message
    >
    > news:...
    >
    > | On Feb 14, 5:45 pm, "agent-s" <> 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
     
    John Machin, Feb 14, 2007
    #9
  10. 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
     
    Michael Bentley, Feb 14, 2007
    #10
  11. agent-s

    Ben Finney Guest

    Michael Bentley <> writes:

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

    --
    \ "Experience is that marvelous thing that enables you to |
    `\ recognize a mistake when you make it again." -- Franklin P. |
    _o__) Jones |
    Ben Finney
     
    Ben Finney, Feb 14, 2007
    #11
  12. On 2007-02-14, Terry Reedy <> wrote:

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

    --
    Grant Edwards grante Yow! I'm EXCITED!! I want
    at a FLANK STEAK WEEK-END!! I
    visi.com think I'm JULIA CHILD!!
     
    Grant Edwards, Feb 14, 2007
    #12
  13. On Wed, 14 Feb 2007 23:05:25 +1100, Ben Finney
    <> declaimed the following in
    comp.lang.python:

    > Michael Bentley <> writes:
    >
    > > # 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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Feb 14, 2007
    #13
  14. agent-s

    Terry Reedy Guest

    "John Machin" <> wrote in message
    news:...
    || > | On Feb 14, 5:45 pm, "agent-s" <> 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 .....?

    agent-s, as I though was obvious.
     
    Terry Reedy, Feb 15, 2007
    #14
  15. agent-s

    agent-s Guest

    Wow. I didn't realize you guys took this so seriously.
     
    agent-s, Feb 23, 2007
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chip
    Replies:
    6
    Views:
    2,651
    E. Robert Tisdale
    Jan 8, 2005
  2. Replies:
    0
    Views:
    848
  3. bdb112
    Replies:
    45
    Views:
    1,373
    jazbees
    Apr 29, 2009
  4. Jesper Rønn-Jensen
    Replies:
    5
    Views:
    161
    Jesper Rønn-Jensen
    Jul 23, 2007
  5. Peter Alvin
    Replies:
    4
    Views:
    106
    Brian Candler
    Sep 29, 2008
Loading...

Share This Page