What happens when loop gets to the end?

break, continue, loops, python

Solution

Python offers an `else` statement for the for loop that is executed if the loop ends without being broken:

for let in llistL:
    if let == letter:
        print("Found it!")
        break
else:
    print("nope could'nt find it")

That would be the "scholar way" for a `for` loop, however if you just test the presence of an element in a list, Arkady's answer is the one to follow.

Problem

I am python beginner, with no previous programming knowledge. I apologize for the name of the topic, but I simply could not make a better one. Here is what I want: ``` letter = "w" # search for this letter in the list bellow listL = ["z","b","y","a","c"] for let in listL: if let == letter: print "found it" break else: if let == listL[-1]: print "nope can't find it" continue ``` I have a list of letters, and I want to search for a particular letter in that list. If I find the letter, then everything is ok, and the for loop should stop. If I do not find it, I would like the loop to stop that current iteration, and try with the next letter in the list. If not a single letter in the list has that particular letter, then it should print "nope can't find it". The upper code works. But I was wondering if this could be wrote a bit clearly? And by clearly I do not mean "advanced", but scholar way, an-example-from-the-book way. Thank you.

Original source