開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のIII部(ステートメント)の13章(while ループと for ループ)練習問題3.を解いてみる。
その他参考書籍
3.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
a = ['a','b','c','d','e']
for x in a:
print(x)
print()
# forループと同じ事をiter関数、next関数を使用して行ってみる
# stopIteration例外が発生したら終了
try:
b = iter(a)
print(next(b))
print(next(b))
print(next(b))
print(next(b))
print(next(b))
print(next(b))
print(next(b))
print(next(b))
print(next(b))
print(next(b))
except Exception:
import sys
print(sys.exc_info())
else:
print("例外発生無し")
finally:
print("終了")
入出力結果(Terminal)
$ ./sample.py a b c d e a b c d e (<class 'StopIteration'>, StopIteration(), <traceback object at 0x10c3ba5f0>) 終了 $
0 コメント:
コメントを投稿