How to catch StopIteration?

C

ccy56781

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
 
J

John Salerno

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! :)
 
L

Lie

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.
 
L

Lie

(snip)

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.
 
C

Chris

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.
 
C

Chris

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top