Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Question on for loop

Reply
Thread Tools

Question on for loop

 
 
newtopython
Guest
Posts: n/a
 
      03-04-2013
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
 
Reply With Quote
 
 
 
 
leo kirotawa
Guest
Posts: n/a
 
      03-04-2013
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 <(E-Mail Removed)> 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)

日本語の*生です。
コンピュータサイエンスの*位.

 
Reply With Quote
 
 
 
 
Joel Goldstick
Guest
Posts: n/a
 
      03-04-2013
On Mon, Mar 4, 2013 at 7:18 AM, newtopython <(E-Mail Removed)> 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

 
Reply With Quote
 
Ricardo Aroz
Guest
Posts: n/a
 
      03-04-2013
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.

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      03-04-2013
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
 
Reply With Quote
 
Bryan Devaney
Guest
Posts: n/a
 
      03-04-2013
> 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.
 
Reply With Quote
 
Rick Johnson
Guest
Posts: n/a
 
      03-04-2013
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.
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      03-04-2013
On Mon, Mar 4, 2013 at 7:34 AM, Bryan Devaney <(E-Mail Removed)> 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.
 
Reply With Quote
 
Bryan Devaney
Guest
Posts: n/a
 
      03-05-2013
On Monday, March 4, 2013 4:37:11 PM UTC, Ian wrote:
> On Mon, Mar 4, 2013 at 7:34 AM, Bryan Devaney <(E-Mail Removed)> 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.
 
Reply With Quote
 
Bryan Devaney
Guest
Posts: n/a
 
      03-05-2013
On Monday, March 4, 2013 4:37:11 PM UTC, Ian wrote:
> On Mon, Mar 4, 2013 at 7:34 AM, Bryan Devaney <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
newbie question: for loop within for loop confusion takayuki Python 17 06-17-2008 09:49 PM
newbie: for loop within for loop question takayuki Python 2 06-16-2008 03:22 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
Loop the loop... =?Utf-8?B?VGltOjouLg==?= ASP .Net 2 02-16-2005 12:21 PM



Advertisments