-3

i've this code, how to force the exit of for loop?:

    try:
            for i in list:
                    with open(i, 'r') as f:
                            json_data = json.load(f)
                    if something == some:
                            print("ddddddddddddddd")

                    else:
                            print("Check successful!!!")
    except IOError:
           print("exception")

i wuold block after print("dddddddd") how can i force the exit?

Luca Deila
  • 29
  • 4

1 Answers1

0

To exit a for or while loop, use the keyword break (docs). This works for "infinite" loops too (like the one below). Example:

n = 0
while True:
    print("Running")
    if n == 3:
        break
    n += 1

will print:

Running
Running
Running
Running

(since it'll run when n is 0, 1, 2 and 3 -> 4 times)

mozi_h
  • 41
  • 6