Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #40441 > unrolled thread
| Started by | newtopython <roshen.sethna@gmail.com> |
|---|---|
| First post | 2013-03-04 04:18 -0800 |
| Last post | 2013-03-04 10:21 -0300 |
| Articles | 10 — 8 participants |
Back to article view | Back to comp.lang.python
Question on for loop newtopython <roshen.sethna@gmail.com> - 2013-03-04 04:18 -0800
Re: Question on for loop leo kirotawa <kirotawa@gmail.com> - 2013-03-04 09:59 -0300
Re: Question on for loop Joel Goldstick <joel.goldstick@gmail.com> - 2013-03-04 08:04 -0500
Re: Question on for loop Dave Angel <davea@davea.name> - 2013-03-04 08:36 -0500
Re: Question on for loop Bryan Devaney <bryan.devaney@gmail.com> - 2013-03-04 06:34 -0800
Re: Question on for loop Ian Kelly <ian.g.kelly@gmail.com> - 2013-03-04 09:37 -0700
Re: Question on for loop Bryan Devaney <bryan.devaney@gmail.com> - 2013-03-05 03:12 -0800
Re: Question on for loop Bryan Devaney <bryan.devaney@gmail.com> - 2013-03-05 03:12 -0800
Re: Question on for loop Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-04 07:41 -0800
Re: Question on for loop Ricardo Aráoz <ricaraoz@gmail.com> - 2013-03-04 10:21 -0300
| From | newtopython <roshen.sethna@gmail.com> |
|---|---|
| Date | 2013-03-04 04:18 -0800 |
| Subject | Question on for loop |
| Message-ID | <d5f0feb6-533a-445b-a89a-8113c1668e9d@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | leo kirotawa <kirotawa@gmail.com> |
|---|---|
| Date | 2013-03-04 09:59 -0300 |
| Message-ID | <mailman.2834.1362402009.2939.python-list@python.org> |
| In reply to | #40441 |
[Multipart message — attachments visible in raw view] — view raw
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 <roshen.sethna@gmail.com> 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) 日本語の学生です。 コンピュータサイエンスの学位.
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2013-03-04 08:04 -0500 |
| Message-ID | <mailman.2835.1362402307.2939.python-list@python.org> |
| In reply to | #40441 |
[Multipart message — attachments visible in raw view] — view raw
On Mon, Mar 4, 2013 at 7:18 AM, newtopython <roshen.sethna@gmail.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-03-04 08:36 -0500 |
| Message-ID | <mailman.2837.1362404218.2939.python-list@python.org> |
| In reply to | #40441 |
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
[toc] | [prev] | [next] | [standalone]
| From | Bryan Devaney <bryan.devaney@gmail.com> |
|---|---|
| Date | 2013-03-04 06:34 -0800 |
| Message-ID | <7345f1f5-d958-4fef-ad50-81fb526b4f2f@googlegroups.com> |
| In reply to | #40441 |
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2013-03-04 09:37 -0700 |
| Message-ID | <mailman.2848.1362415074.2939.python-list@python.org> |
| In reply to | #40448 |
On Mon, Mar 4, 2013 at 7:34 AM, Bryan Devaney <bryan.devaney@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Bryan Devaney <bryan.devaney@gmail.com> |
|---|---|
| Date | 2013-03-05 03:12 -0800 |
| Message-ID | <ebe74c96-68af-46b9-88f1-107e7b7e8c6d@googlegroups.com> |
| In reply to | #40463 |
On Monday, March 4, 2013 4:37:11 PM UTC, Ian wrote: > On Mon, Mar 4, 2013 at 7:34 AM, Bryan Devaney <bryan.devaney@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Bryan Devaney <bryan.devaney@gmail.com> |
|---|---|
| Date | 2013-03-05 03:12 -0800 |
| Message-ID | <mailman.2877.1362481970.2939.python-list@python.org> |
| In reply to | #40463 |
On Monday, March 4, 2013 4:37:11 PM UTC, Ian wrote: > On Mon, Mar 4, 2013 at 7:34 AM, Bryan Devaney <bryan.devaney@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
|---|---|
| Date | 2013-03-04 07:41 -0800 |
| Message-ID | <f8b49fee-7cd3-4fcc-9303-666bd6d53d51@googlegroups.com> |
| In reply to | #40441 |
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 over a 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 write down the steps required to compare two sequences in real life. 1. Create a list of "letters guessed" and a string representing the "secret word". secretWord = 'multiplicity' lettersGuessed = 'aeiouy' 2. For each letter in "secretWord", look in "lettersGuessed" and see if you can 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 user to 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.
[toc] | [prev] | [next] | [standalone]
| From | Ricardo Aráoz <ricaraoz@gmail.com> |
|---|---|
| Date | 2013-03-04 10:21 -0300 |
| Message-ID | <mailman.2851.1362417534.2939.python-list@python.org> |
| In reply to | #40441 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web