473,382 Members | 1,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to catch StopIteration?

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 14653
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
Lie
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
Lie
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 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
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
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
Jun 27 '08 #6
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 Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Peter Otten | last post by:
To confuse a newbies and old hands alike, Bengt Richter wrote: > Need something more straightforward, e.g., a wrapped one-liner: > > >>> def guess(n=3): print ("You're right!", 'No more tries...
10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
5
by: tkpmep | last post by:
I create list of files, open each file in turn, skip past all the blank lines, and then process the first line that starts with a number (see code below) filenames=glob.glob("C:/*.txt") for fn...
5
by: Kris Kowal | last post by:
I had a thought that might be pepworthy. Might we be able to break outer loops using an iter-instance specific StopIteration type? This is the desired, if not desirable, syntax:: import...
0
by: Luis Zarrabeitia | last post by:
Quoting Kris Kowal <kris.kowal@cixar.com>: I must say, I don't even like the idea of having a 'break', but I kind oflike this proposal. However, it may be ambiguous if the outer and inner...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.