Question on for loop

Discussion in 'Python' started by newtopython, Mar 4, 2013.

  1. newtopython

    newtopython Guest

    Hi all,

    I'm super new to python, just fyi.

    In the piece of code below, secretWord is a string and lettersGuessed is a list. I'm trying to find out if ALL the characters of secretWord are included in lettersGuessed, even if there are additional values in the lettersGuessed list that aren't in secretWord.

    What this code is doing is only checking the first character of secretWord and then returning True or False. How do I get it to iterate through ALL ofthe characters of secretWord?

    for character in secretWord:
    if character not in lettersGuessed:
    return True
    return False

    Thanks!

    Ro
     
    newtopython, Mar 4, 2013
    #1
    1. Advertising

  2. newtopython

    leo kirotawa Guest

    In fact this code is already doing what you want, but if the second
    character, by example, is not in secrectWord it'll jump out of the for and
    return. If you want that interact through the all characters and maybe
    count how many them are in the secrectWord, just take of the return there
    or do some list comprehension like: [ r for r in secrecWord if r in
    lettersGuessed] .



    []'s



    On Mon, Mar 4, 2013 at 9:18 AM, newtopython <> wrote:

    > Hi all,
    >
    > I'm super new to python, just fyi.
    >
    > In the piece of code below, secretWord is a string and lettersGuessed is a
    > list. I'm trying to find out if ALL the characters of secretWord are
    > included in lettersGuessed, even if there are additional values in the
    > lettersGuessed list that aren't in secretWord.
    >
    > What this code is doing is only checking the first character of secretWord
    > and then returning True or False. How do I get it to iterate through ALL of
    > the characters of secretWord?
    >
    > for character in secretWord:
    > if character not in lettersGuessed:
    > return True
    > return False
    >
    > Thanks!
    >
    > Ro
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    Leônidas S. Barbosa (Kirotawa)

    Engenheiro de Software - IBM (LTC - Linux Technology Center)
    MsC Sistemas e Computação
    Bacharel em Ciências da Computação.

    blog nerd: corecode.wordpress.com/

    <http://corecode.wordpress.com/>User linux : #480879

    "Mais sábio é aquele que sabe que não sabe" (Sócrates)
    "smile and wave" - =D + o/ (Penguins of Madagascar)

    日本語ã®å­¦ç”Ÿã§ã™ã€‚
    コンピュータサイエンスã®å­¦ä½.
     
    leo kirotawa, Mar 4, 2013
    #2
    1. Advertising

  3. On Mon, Mar 4, 2013 at 7:18 AM, newtopython <> wrote:

    > Hi all,
    >
    > I'm super new to python, just fyi.
    >


    Welcome. Next time write a better subject line, and be sure the code you
    post is actually the code you are running. Provide the results you want
    and what you get. Provide the traceback if there is one

    >
    > In the piece of code below, secretWord is a string and lettersGuessed is a
    > list. I'm trying to find out if ALL the characters of secretWord are
    > included in lettersGuessed, even if there are additional values in the
    > lettersGuessed list that aren't in secretWord.
    >
    > What this code is doing is only checking the first character of secretWord
    > and then returning True or False. How do I get it to iterate through ALL of
    > the characters of secretWord?
    >
    > for character in secretWord:
    > if character not in lettersGuessed:
    >


    I am guessing that the next two lines are actually indented in your script
    so I am changing them here


    return True
    > return False
    >
    > The first time your if block is checked it will return True or False.

    Since you haven't shown this code in a function, as written it won't run at
    all.

    Your question makes no sense. What would it mean to look through each
    character and return True or False? What would make the result True? All
    matches, some matches?

    > Thanks!
    >
    > Ro
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    Joel Goldstick
    http://joelgoldstick.com
     
    Joel Goldstick, Mar 4, 2013
    #3
  4. El 04/03/13 09:18, newtopython escribió:
    > Hi all,
    >
    > I'm super new to python, just fyi.
    >
    > In the piece of code below, secretWord is a string and lettersGuessed is a list. I'm trying to find out if ALL the characters of secretWord are included in lettersGuessed, even if there are additional values in the lettersGuessed list that aren't in secretWord.
    >
    > What this code is doing is only checking the first character of secretWord and then returning True or False. How do I get it to iterate through ALL of the characters of secretWord?
    >
    > for character in secretWord:
    > if character not in lettersGuessed:
    > return True
    > return False
    >
    > Thanks!
    >
    > Ro


    Indent the "return True" line so that it is inside the if clause.
     
    Ricardo Aráoz, Mar 4, 2013
    #4
  5. newtopython

    Dave Angel Guest

    On 03/04/2013 07:18 AM, newtopython wrote:
    > Hi all,
    >
    > I'm super new to python, just fyi.


    Welcome to the Python list.

    >
    > In the piece of code below, secretWord is a string and lettersGuessed is a list. I'm trying to find out if ALL the characters of secretWord are included in lettersGuessed, even if there are additional values in the lettersGuessed list that aren't in secretWord.
    >
    > What this code is doing is only checking the first character of secretWord and then returning True or False. How do I get it to iterate through ALL of the characters of secretWord?
    >
    > for character in secretWord:
    > if character not in lettersGuessed:
    > return True
    > return False
    >


    Please post a complete sample when possible, and make sure you
    copy/paste it, not just retype it and hope. As written, it'll throw an
    exception when return is encountered. But before that, it'll complain
    about the indentation of the return True.

    Perhaps you have something like:

    def has_some_behavior(secretWord, lettersGuessed):
    for character in secretWord:
    if character not in lettersGuessed:
    return True
    return False

    If so, please copy the whole thing from your code, and explain just how
    you call it (what arguments are passed), what it returned, and what's
    wrong with that behavior.


    --
    DaveA
     
    Dave Angel, Mar 4, 2013
    #5
  6. > if character not in lettersGuessed:
    >
    > return True
    >
    > return False


    assuming a function is being used to pass each letter of the letters guessed inside a loop itself that only continues checking if true is returned, then that could work.

    It is however more work than is needed.

    If you made secretword a list,you could just

    set(secretword)&set(lettersguessed)

    and check the result is equal to secretword.
     
    Bryan Devaney, Mar 4, 2013
    #6
  7. newtopython

    Rick Johnson Guest

    On Monday, March 4, 2013 6:18:20 AM UTC-6, newtopython wrote:

    [Note: Post has be logically re-arranged for your comprehensive pleasures]

    > for character in secretWord:
    > if character not in lettersGuessed:
    > return True
    > return False
    >
    > What this code is doing is only checking the first
    > character of secretWord and then returning True or False.
    > How do I get it to iterate through ALL of the characters
    > of secretWord?


    Your code is a fine example of: "attempting to solve too many problems at the same time". If you are having trouble understanding how to iterate overa sequence, then why would you complicate that learning experience by injecting other unsolved problems into the mix? First, solve the iteration problem. Then expand.

    ## START INTERACTIVE SESSION ##
    py> s = 'multiplicity'
    py> for char in s:
    ... print char
    m
    u
    l
    t
    i
    p
    l
    i
    c
    i
    t
    y
    ## END INTERACTIVE SESSION ##

    Now, we have a simple base from which to build!

    > In the piece of code [ABOVE], secretWord is a string and
    > lettersGuessed is a list. I'm trying to find out if ALL
    > the characters of secretWord are included in
    > lettersGuessed, even if there are additional values in the
    > lettersGuessed list that aren't in secretWord.


    First, step away from your interpreter! Now, grab a pen and paper and writedown the steps required to compare two sequences in real life.

    1. Create a list of "letters guessed" and a string representing the "secretword".

    secretWord = 'multiplicity'
    lettersGuessed = 'aeiouy'

    2. For each letter in "secretWord", look in "lettersGuessed" and see if youcan find the letter, then make a note of your findings. If the letter is in IN both sequences, write "[letter]=True", if not, write "[letter]=False".

    However, this algorithm is rather naive. What happens if one or both list contain the same letter numerous times (f.e. "multiplicity" has 3 "i" chars)? Do we want the user to provide a guess for all three "i" chars, or will just a single guess al la "price is right" will do the trick?

    Also, do we care about the "char order"? Or are we merely allowing the userto guess all the letters of the word in ANY order (that seems to be your intent here!)?

    In any event i am not going to just "gift wrap" and answer for you. There are many methods of solving this problem, some are elegant, some or not elegant, some use built-in functions, some use list comprehensions, and some could just use a for loop and a single built-in function. I would highly suggest that you figure this out using the latter. Until you can achieve this, forget about list comprehension or any advanced stuff.

    But most importantly: Build your code in small incremental steps and solve ONE issue at a time. This is the path of a wise problem solver.
     
    Rick Johnson, Mar 4, 2013
    #7
  8. newtopython

    Ian Kelly Guest

    On Mon, Mar 4, 2013 at 7:34 AM, Bryan Devaney <> wrote:
    >> if character not in lettersGuessed:
    >>
    >> return True
    >>
    >> return False

    >
    > assuming a function is being used to pass each letter of the letters guessed inside a loop itself that only continues checking if true is returned, then that could work.
    >
    > It is however more work than is needed.
    >
    > If you made secretword a list,you could just
    >
    > set(secretword)&set(lettersguessed)
    >
    > and check the result is equal to secretword.


    Check the result is equal to set(secretword), I think you mean.

    set(secretword).issubset(set(lettersguessed))

    might be slightly more efficient, since it would not need to build and
    return an intersection set.

    One might also just do:

    all(letter in lettersguessed for letter in secretword)

    Which will be efficient if lettersguessed is already a set.
     
    Ian Kelly, Mar 4, 2013
    #8
  9. On Monday, March 4, 2013 4:37:11 PM UTC, Ian wrote:
    > On Mon, Mar 4, 2013 at 7:34 AM, Bryan Devaney <> wrote:
    >
    > >> if character not in lettersGuessed:

    >
    > >>

    >
    > >> return True

    >
    > >>

    >
    > >> return False

    >
    > >

    >
    > > assuming a function is being used to pass each letter of the letters guessed inside a loop itself that only continues checking if true is returned, then that could work.

    >
    > >

    >
    > > It is however more work than is needed.

    >
    > >

    >
    > > If you made secretword a list,you could just

    >
    > >

    >
    > > set(secretword)&set(lettersguessed)

    >
    > >

    >
    > > and check the result is equal to secretword.

    >
    >
    >
    > Check the result is equal to set(secretword), I think you mean.
    >
    >
    >
    > set(secretword).issubset(set(lettersguessed))
    >
    >
    >
    > might be slightly more efficient, since it would not need to build and
    >
    > return an intersection set.
    >
    >
    >
    > One might also just do:
    >
    >
    >
    > all(letter in lettersguessed for letter in secretword)
    >
    >
    >
    > Which will be efficient if lettersguessed is already a set.


    You are correct. sorry for the misleading answer, was digging through old shell scripts all day yesterday and brain was obviously not not the better for it.
     
    Bryan Devaney, Mar 5, 2013
    #9
  10. On Monday, March 4, 2013 4:37:11 PM UTC, Ian wrote:
    > On Mon, Mar 4, 2013 at 7:34 AM, Bryan Devaney <> wrote:
    >
    > >> if character not in lettersGuessed:

    >
    > >>

    >
    > >> return True

    >
    > >>

    >
    > >> return False

    >
    > >

    >
    > > assuming a function is being used to pass each letter of the letters guessed inside a loop itself that only continues checking if true is returned, then that could work.

    >
    > >

    >
    > > It is however more work than is needed.

    >
    > >

    >
    > > If you made secretword a list,you could just

    >
    > >

    >
    > > set(secretword)&set(lettersguessed)

    >
    > >

    >
    > > and check the result is equal to secretword.

    >
    >
    >
    > Check the result is equal to set(secretword), I think you mean.
    >
    >
    >
    > set(secretword).issubset(set(lettersguessed))
    >
    >
    >
    > might be slightly more efficient, since it would not need to build and
    >
    > return an intersection set.
    >
    >
    >
    > One might also just do:
    >
    >
    >
    > all(letter in lettersguessed for letter in secretword)
    >
    >
    >
    > Which will be efficient if lettersguessed is already a set.


    You are correct. sorry for the misleading answer, was digging through old shell scripts all day yesterday and brain was obviously not not the better for it.
     
    Bryan Devaney, Mar 5, 2013
    #10
    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. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,385
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,240
    Tim Slattery
    Mar 30, 2005
  3. takayuki
    Replies:
    2
    Views:
    281
    Calvin Spealman
    Jun 16, 2008
  4. takayuki
    Replies:
    17
    Views:
    472
    John Salerno
    Jun 17, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page