Use break and continue to do this. Breaking nested loops can be done in Python using the following:
for a in range(...):
for b in range(..):
if some condition:
# break the inner loop
break
else:
# will be called if the previous loop did not end with a `break`
continue
# but here we end up right after breaking the inner loop, so we can
# simply break the outer loop as well
break
Another way is to wrap everything in a function and use return to escape from the loop.
python - Loop stops after first iteration - Stack Overflow
How can we exit a loop?
How do I end a For or While loop without using break?
For Loop for n iterations-Python - Stack Overflow
You're returning immediately in both the if and else blocks. That ends the loop in both cases.
You should only return in the if block. If you make it through the entire loop without returning, you know that all the words are title case.
def all_title_case(word_list):
for word in word_list:
if not word.istitle():
return False
return True
You can also use the all() function instead of a loop.
def all_title_case(word_list):
return all(word.istitle() for word in word_list)
Return statement ends the execution of your function, if you return True only when your for iteration is done you will have what you want
In other words your return statement ends your for loop, you can read some about it on this question: How to use a return statement in a for loop?
word_list=["ABC", "abc", "Abc"]
def all_title_case(word_list):
for word in word_list:
if not word.istitle():
return False
return True
print(all_title_case(word_list))
for filename in filenames[:100]:
outline= getinfo(filename)
outfile.write(outline)
The list slice filenames[:100] will truncate the list of file names to just the first 100 elements.
Keep a counter for your for loop. When your counter reaches, 100, break
counter = 0
for filename in filenames:
if counter == 100:
break
outline= getinfo(filename)
outfile.write(outline)
counter += 1