Python Forum
For loop within a for loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: For loop within a for loop (/thread-1913.html)



For loop within a for loop - tre - Feb-04-2017

Python 2.7.10
I can't seem to figure out the way to do this.  Here is the code

something = "go dumb cat"

words = something.split()
word_one = ('noun', 'cat')
word_two = ('verb', 'go')
sentence = [word_one, word_two]


listing = []
for each in words:
    for x in sentence:
        if each in x:
            pass
            listing.append(x)
        else:
           listing.append(('error', x[1]))

print listing
I'm not surprised that the above didn't do what I want. Here is the output.

Output:
[('error', 'cat'), ('verb', 'go'), ('error', 'cat'), ('error', 'go'), ('noun', 'cat'), ('error', 'go')]
The else is where I get the output I don't want, with just the if I get the following (and the more I try to figure it out, the more I'm surprised that this part works how I want it to)
Output:
[('verb', 'go'), ('noun', 'cat')]
This is what I would want it to do. 

Output:
('verb', 'go'), ('noun', 'cat'), ('error', 'dumb')]



RE: For loop within a for loop - ichabod801 - Feb-04-2017

You only want to append error if you didn't find the word in any of the parts of speech. Currently you are appending it for each part of speech it's not in. The standard way to do this is with the break statement, and moving the else clause to be part of the for loop:

for number in sequence:
    if number % 2:
        print('Odd number found.')
        break
else:
    print('No odd numbers found.')
The else clause on a for loop executes when the loop exits without a break statement. A good way to think of this is that you are using the for loop to find something. If you find it, you break out of the loop, otherwise you execute the else clause.


RE: For loop within a for loop - tre - Feb-04-2017

Just to clarify, I put this in the homework section because the lesson I'm following seems to be using tuples for something I don't think you would use tuples for when actually designing something and I didn't want any focus to be on that. So my academic integrity won't be compromised straight-up working pieces of code as answer. (not that I don't mind trying to figure things out)

Also there is an error in my code before, where I put the wrong variable: Here is the corrected part

for each in words:
   for x in sentence:
       if each in x:
           pass
           listing.append(x)
       else:
          listing.append(('error', each))
(Feb-04-2017, 12:02 PM)ichabod801 Wrote: Currently you are appending it for each part of speech it's not in.

Ok, I understand this part, but I don't understand the solution yet. So will a for loop within a loop not do what I want? I think I've come to the that conclusion, but what's the best way to do this then?

What I'd like to do is for each word in words, if the word is found in one of the tuples, then that whole tuple gets appended to the listing list. If the word is not found then a tuple containing 'error' and the word not found gets appended to the listing list. You probably understand this already, and I just have a mental block. From you're answer, I'd say the solution is to go with if, elif, else like below, but surely there is a better way?

something = "go dumb cat"

words = something.split()
word_one = ('noun', 'cat')
word_two = ('verb', 'go')
sentence = [word_one, word_two]

listing = []
for each in words:
   if each in word_one:
       listing.append(word_one)
   elif each in word_two:
       listing.append(word_two)
   else: 
       listing.append(('error', each))
       
   
print listing



RE: For loop within a for loop - ichabod801 - Feb-04-2017

(Feb-04-2017, 03:20 PM)tre Wrote: So will a for loop within a loop not do what I want?

No, it can do it. I was saying make your for x in sentence: loop be like the loop I posted. So the if clause in that loop would have a break statement, and the else clause would go with the for statement instead of the if statement. BTW, the pass statement does nothing. So you might as well take it out.

Note that this will work if each word is only in one part of speech list. If you want to catch words that have multiple parts of speech, you can still do it; you will just need to do some tracking of whether things get appended yet.


RE: For loop within a for loop - tre - Feb-04-2017

(Feb-04-2017, 04:13 PM)ichabod801 Wrote: BTW, the pass statement does nothing. So you might as well take it out.
Hmm, must be a relic of when I was first putting the example together.

Thanks, I got it working, it looked like it didn't do what I wanted because I tried it before fixing my error, I was telling it to do the wrong thing. It's still breaking my brain though. I tried to draw/diagram out what happens but I get so confused. Time to go back and read up on else clauses on for loops I think. Thanks for your help!