464,835 Members | 1,048 Online
Need help? Post your question and get tips & solutions from a community of 464,835 IT Pros & Developers. It's quick & easy.

# How to catch StopIteration?

 P: n/a I'm writing to see calcuration process. And so, I can't catch StopIteration... What is mistake? def collatz(n): r=[] while n>1: r.append(n) n = 3*n+1 if n%2 else n/2 yield r for i, x in enumerate(collatz(13)): try: last = x[:i+1] print x[:i+1] except StopIteration: print last.appnd(1) Output: [13] [13, 40] [13, 40, 20] [13, 40, 20, 10] [13, 40, 20, 10, 5] [13, 40, 20, 10, 5, 16] [13, 40, 20, 10, 5, 16, 8] [13, 40, 20, 10, 5, 16, 8, 4] [13, 40, 20, 10, 5, 16, 8, 4, 2] last.appnd(1) <= [13, 40, 20, 10, 5, 16, 8, 4, 2, 1] # i want this list Jun 27 '08 #1
6 Replies

 P: n/a cc******@gmail.com wrote: for i, x in enumerate(collatz(13)): try: last = x[:i+1] print x[:i+1] except StopIteration: print last.appnd(1) My guess would be because StopIteration is raised when control returns to the for loop and sees that it has nothing else left. At that point, you aren't in the try statement and so the exception can't be caught that way. But I should let the experts answer. I need to go to bed anyway! :) Jun 27 '08 #2

 P: n/a On Jun 17, 10:50*am, ccy56...@gmail.com wrote: I'm writing to see calcuration process. And so, I can't catch StopIteration... What is mistake? def collatz(n): * r=[] * while n>1: * * r.append(n) * * n = 3*n+1 if n%2 else n/2 * * yield r for i, x in enumerate(collatz(13)): * try: * * last = x[:i+1] * * print x[:i+1] * except StopIteration: * * print last.appnd(1) Output: [13] [13, 40] [13, 40, 20] [13, 40, 20, 10] [13, 40, 20, 10, 5] [13, 40, 20, 10, 5, 16] [13, 40, 20, 10, 5, 16, 8] [13, 40, 20, 10, 5, 16, 8, 4] [13, 40, 20, 10, 5, 16, 8, 4, 2] last.appnd(1) <= [13, 40, 20, 10, 5, 16, 8, 4, 2, 1] *# i want this list In a for-loop, StopIteration is caught by the for-loop for your convenience (so you don't need to set up try-except of your own) def collatz(n): r=[] while n>1: r.append(n) n = 3*n+1 if n%2 else n/2 yield r for i, x in enumerate(collatz(13)): last = x[:i+1] print x[:i+1] last.append(1) print last PS: btw, you can't write 'print last.append(1)' because list operations like append doesn't return itself. Jun 27 '08 #3

 P: n/a On Jun 17, 12:36*pm, Lie In a for-loop, StopIteration is caught by the for-loop for your convenience (so you don't need to set up try-except of your own) (snip) To clarify, when the for-loop catches StopIteration, the for-loop quits and stop looping, continuing to the next statement after the loop block. To be more accurate, in your case, the StopIteration is caught by the enumerate() when it is enumerating collatz. The for-loop then loops the generator expression returned by enumerate, when enumerate runs out of things to return (i.e. when enumerate caught StopIteration raised by collatz), enumerate raise StopIteration which is caught by the for-loop. PS: btw, you can't write 'print last.append(1)' because list operations like append doesn't return itself. To clarify, list append returns 'None' which, in python, means it is a method/sub. In actuality, python doesn't recognize the difference between method and function. Jun 27 '08 #4

 P: n/a On Jun 17, 5:50*am, ccy56...@gmail.com wrote: I'm writing to see calcuration process. And so, I can't catch StopIteration... What is mistake? def collatz(n): * r=[] * while n>1: * * r.append(n) * * n = 3*n+1 if n%2 else n/2 * * yield r for i, x in enumerate(collatz(13)): * try: * * last = x[:i+1] * * print x[:i+1] * except StopIteration: * * print last.appnd(1) Output: [13] [13, 40] [13, 40, 20] [13, 40, 20, 10] [13, 40, 20, 10, 5] [13, 40, 20, 10, 5, 16] [13, 40, 20, 10, 5, 16, 8] [13, 40, 20, 10, 5, 16, 8, 4] [13, 40, 20, 10, 5, 16, 8, 4, 2] last.appnd(1) <= [13, 40, 20, 10, 5, 16, 8, 4, 2, 1] *# i want this list def collatz(n): r=[] while n>1: r.append(n) n = 3*n+1 if n%2 else n/2 yield r i = 1 while 1: try: last = x[:i] print x[:i] i += 1 except StopIteration last.append(1) break You will have to control the for loop yourself otherwise the StopIteration is handled for you. Jun 27 '08 #5

 P: n/a On Jun 17, 8:43*am, Chris 1: * * r.append(n) * * n = 3*n+1 if n%2 else n/2 * * yield r for i, x in enumerate(collatz(13)): * try: * * last = x[:i+1] * * print x[:i+1] * except StopIteration: * * print last.appnd(1) Output: [13] [13, 40] [13, 40, 20] [13, 40, 20, 10] [13, 40, 20, 10, 5] [13, 40, 20, 10, 5, 16] [13, 40, 20, 10, 5, 16, 8] [13, 40, 20, 10, 5, 16, 8, 4] [13, 40, 20, 10, 5, 16, 8, 4, 2] last.appnd(1) <= [13, 40, 20, 10, 5, 16, 8, 4, 2, 1] *# i want this list def collatz(n): * r=[] * while n>1: * * r.append(n) * * n = 3*n+1 if n%2 else n/2 * * yield r i = 1 while 1: * * try: * * * * last = x[:i] * * * * print x[:i] * * * * i += 1 * * except StopIteration * * * * last.append(1) * * * * break You will have to control the for loop yourself otherwise the StopIteration is handled for you. forgot to put x = collatz(13) before the while loop starts and a x.next() inside the while, but hopefully you get the point :p Jun 27 '08 #6

 P: n/a cc******@gmail.com 1: r.append(n) n = 3*n+1 if n%2 else n/2 yield r for i, x in enumerate(collatz(13)): try: last = x[:i+1] print x[:i+1] except StopIteration: print last.appnd(1) Output: [13] [13, 40] [13, 40, 20] [13, 40, 20, 10] [13, 40, 20, 10, 5] [13, 40, 20, 10, 5, 16] [13, 40, 20, 10, 5, 16, 8] [13, 40, 20, 10, 5, 16, 8, 4] [13, 40, 20, 10, 5, 16, 8, 4, 2] last.appnd(1) <= [13, 40, 20, 10, 5, 16, 8, 4, 2, 1] # i want this list I would have thought you want this... >>for i, x in enumerate(collatz(13)): .... last = x[:i+1] .... print x[:i+1] .... else: .... last.append(1) .... print last .... [13] [13, 40] [13, 40, 20] [13, 40, 20, 10] [13, 40, 20, 10, 5] [13, 40, 20, 10, 5, 16] [13, 40, 20, 10, 5, 16, 8] [13, 40, 20, 10, 5, 16, 8, 4] [13, 40, 20, 10, 5, 16, 8, 4, 2] [13, 40, 20, 10, 5, 16, 8, 4, 2, 1] -- Nick Craig-Wood

### This discussion thread is closed

Replies have been disabled for this discussion.