Question on for loop

N

newtopython

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
 
L

leo kirotawa

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



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



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

日本語ã®å­¦ç”Ÿã§ã™ã€‚
コンピュータサイエンスã®å­¦ä½.
 
J

Joel Goldstick

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?
 
R

Ricardo Aráoz

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

Dave Angel

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

Bryan Devaney

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

Rick Johnson

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

Ian Kelly

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

Bryan Devaney

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

Bryan Devaney

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top