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  
Share this Question
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! :)  
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 forloop, StopIteration is caught by the forloop for your
convenience (so you don't need to set up tryexcept 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.  
P: n/a

On Jun 17, 12:36*pm, Lie <Lie.1...@gmail.comwrote:
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?
(snip)
>
In a forloop, StopIteration is caught by the forloop for your
convenience (so you don't need to set up tryexcept of your own)
(snip)
To clarify, when the forloop catches StopIteration, the forloop
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 forloop 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 forloop.
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.  
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.  
P: n/a

On Jun 17, 8:43*am, Chris <cwi...@gmail.comwrote:
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.
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  
P: n/a
 cc******@gmail.com <cc******@gmail.comwrote:
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
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 CraigWood <ni**@craigwood.com http://www.craigwood.com/nick   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 13043
 replies: 6
 date asked: Jun 27 '08
