while i < 5:
do stuff
if i == 3:
print "i is 3"
continue
Answer from Matthew on Stack Overflowwhile i < 5:
do stuff
if i == 3:
print "i is 3"
continue
Instead of break use continue
Now, I pretty much never use continue as I find it is usually clearer to rework the code to avoid it. Of course that's really easy in this example, if you have trouble with a more complex example ask about that one.
python - How to stop one or multiple for loop(s) - Stack Overflow
Ending a "For" loop when condition is not met
python - Stop for loop or while loop when condition met - Stack Overflow
python - Stopping a function with a while loop when a certain condition is met - Stack Overflow
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.
There are several ways to do it:
The simple Way: a sentinel variable
n = L[0][0]
m = len(A)
found = False
for i in range(m):
if found:
break
for j in range(m):
if L[i][j] != n:
found = True
break
Pros: easy to understand Cons: additional conditional statement for every loop
The hacky Way: raising an exception
n = L[0][0]
m = len(A)
try:
for x in range(3):
for z in range(3):
if L[i][j] != n:
raise StopIteration
except StopIteration:
pass
Pros: very straightforward Cons: you use Exception outside of their semantic
The clean Way: make a function
def is_different_value(l, elem, size):
for x in range(size):
for z in range(size):
if l[i][j] != elem:
return True
return False
if is_different_value(L, L[0][0], len(A)):
print "Doh"
pros: much cleaner and still efficient cons: yet feels like C
The pythonic way: use iteration as it should be
def is_different_value(iterable):
first = iterable[0][0]
for l in iterable:
for elem in l:
if elem != first:
return True
return False
if is_different_value(L):
print "Doh"
pros: still clean and efficient cons: you reinvdent the wheel
The guru way: use any():
def is_different_value(iterable):
first = iterable[0][0]
return any(cell != first for col in iterable for cell in col)
if is_different_value(L):
print "Doh"
pros: you'll feel empowered with dark powers cons: people that will read you code may start to dislike you
Edit: SOLVED
I was going to post this in the Ask Anything Weekly Monday Thread but it seemed a little long so here goes.
I am trying to end a "for" loop if the condition is not met. Specifically, I need to write a function that will check if a string contains any vowels, if so return True, if not return False. I am struggling with getting my function to return False when the string does not contain any vowels. Right now, my function always returns None if there are no vowels.
The correct answer should return:
TrueTrueFalseFalse
My best answer returns:
TrueTrueNoneFalse
I believe I am getting a "None" because nothing is being returned when the function is run and the string contains no vowels. I think the "continue" loop in Line 13 and 14 are the reason. Or maybe that's not the problem.... I'm so stuck.
I have included the quiz, along with my best attempt at answering it below
EDIT: A big thank you to everyone for your help and explanations!!
#My best attempt
def has_a_vowel(a_str):
#If a_str is empty it will return False
if a_str == "":
return False
#Review every letter in a_str
for letter in a_str:
#If letter is a vowel, return the value True
if letter in "aeiou":
return True
#If letter is not a vowel, continue checking the other letters
elif letter not in "aeiou":
continue
#Here I want to say if there are no vowels in a_str, return false
else:
if letter not in "aeiou":
return False
print(has_a_vowel("abba"))
print(has_a_vowel("beeswax"))
print(has_a_vowel("syzygy"))
print(has_a_vowel(""))You might wish to alter you loop condition. Instead of while True try:
place = places[0]
while place != "LA Dodgers Stadium" and count < len(places):
if ' ' in place:
multi_word += 1
count += 1
place = places[count]
Edit: possibly a better way of writing this might be:
for place in places:
if place == "LA Dodgers Stadium": break
if ' ' in place: multi_word += 1
(pseudo code)
flag = true
while (flag)
...
if (place == LA Dodgers stadium)
{
flag = false
}