Python’da iç içe looplardan çıkmak
# Break out of a nested for loop when 'emma' is reached # Python does not have a goto statement to do this! print # or raise an exception ... try: for list2 in list1: for item in list2: print item if item == 'emma': # raise an exception raise StopIteration() except StopIteration: pass # or use a function and force a return ... def break_nestedloop(list1): for list2 in list1: for item in list2: print item if item == 'emma': # force a return return dummy = break_nestedloop(list1) # returns None
Bir cevap yazın