Python Forum
For loop within a for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loop within a for loop
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
For loop within a for loop - by tre - Feb-04-2017, 05:21 AM
RE: For loop within a for loop - by ichabod801 - Feb-04-2017, 12:02 PM
RE: For loop within a for loop - by tre - Feb-04-2017, 03:20 PM
RE: For loop within a for loop - by ichabod801 - Feb-04-2017, 04:13 PM
RE: For loop within a for loop - by tre - Feb-04-2017, 10:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop to find the best combination/score KoinKoin 21 29,305 Jan-05-2023, 10:31 AM
Last Post: KoinKoin
  Many iterations for loop question adesimone 9 1,937 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,725 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  while loop idddj 8 1,773 Oct-03-2022, 05:03 PM
Last Post: jefsummers
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,873 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Function combining file manipulation and loop Leyo 5 1,869 Mar-23-2022, 09:47 AM
Last Post: Leyo
  Using If Statements Instead of While Loop in Simple Game Program new_coder_231013 5 3,257 Dec-14-2021, 12:23 AM
Last Post: supuflounder
Big Grin for loop nadun 3 1,927 Nov-22-2021, 03:36 PM
Last Post: deanhystad
  How to compile following python loop program reinispl 3 2,003 Oct-27-2021, 01:57 PM
Last Post: DeaD_EyE
  Printing During a Loop apeltes 16 5,434 Oct-21-2021, 12:19 AM
Last Post: apeltes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020