#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Word example issue


    Code:
    def uses_all(word, required):
    for letter in required:
    if letter not in word:
    return False
    return True
    Code:
    def uses_all(word, required):
    for letter in required:
    if letter in word:
    return True
    return False
    Im finding it difficult to see the difference between the two above code examples. Why do they give different answers.
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    608
    Rep Power
    65
    The difference could be in the required block indentations.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0
    they have the same indentation but give different answers for the same inputs
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    608
    Rep Power
    65
    So, what are your indentations?
    Don't overlook the not
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0
    def uses_all(word, required):
    for letter in required:
    if letter not in word:
    return False
    return True

    def uses_all(word, required):
    for letter in required:
    if letter in word:
    return True
    return False

    This is how they are indented.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0
    Code:
    def uses_all(word, required):
        for letter in required:
            if letter not in word:
                return False
        return True
    Code:
    def uses_all(word, required):
        for letter in required:
            if letter in word:
                return True
        return False
    This is the indentation(again).
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    The first return statement is inside the loop, so there are multiple chances to reach it. The second return statement is outside the loop, so it is only reached if the loop completes.

    The first function will return False as soon as it sees a letter that's not in the word. If it doesn't see any, it returns True.

    The second will return True as soon as it sees a letter that is in the word. This means that if any of the required letters are in the word, you'll get True. False will be returned if none of them are.

    Comments on this post

    • Dietrich agrees
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0
    cheers for the reply, i think i understand now.

IMN logo majestic logo threadwatch logo seochat tools logo