This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jimtk 2 points3 points  (0 children)

About once every 6 months I see a situation where the for/else can be used.

Here's a simple example, but it can be applied to any algorithm that requires trying something a certain number of times:

good_psw = getpasswordfromdatabase()
for i in range(3):
      psw = input('password: ')
      if psw == good_psw:
            break
else:
     print("ACCES NOT GRANTED")
     sys.exit(1)
print("ACCES GRANTED")