stopiteration error python

Solutions on MaxInterview for stopiteration error python by the best coders in the world

showing results for - "stopiteration error python"
Russ
16 Jan 2020
1# When an iterator is done, it’s next method raises StopIteration. This exception is 
2# not considered an error. We re-write the given code as follows to catch the 
3# exception and know its type.
4
5Example
6import sys
7try:
8z = [5, 9, 7]
9i = iter(z)
10print i
11print i.next()
12print i.next()
13print i.next()
14print i.next()
15except Exception as e:
16print e
17print sys.exc_type
18Output
19<listiterator object at 0x0000000002AF23C8>
205
219
227
23<type 'exceptions.StopIteration'>