開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
初めてのコンピュータサイエンス(Jennifer Campbell、Paul Gries、Jason Montojo、Greg Wilson(著)長尾 高弘(翻訳))の12章(各種ツール)、12.7(練習問題)、12-14.を解いてみる。
12.7(練習問題)、12-14.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- # 問題の関数は数値limitに負の数値が入力された時に失敗する # (while loopでcurrentが0になることがないから) # 修正 def summation(limit): total = 0 current = limit if limit < 0: while current != 0: total += current current += 1 else: while current != 0: total += current current -= 1 return total if __name__ == '__main__': for limit in [-10, 10]: print('limit={0}'.format(limit)) print(summation(limit))
入出力結果(Terminal)
$ ./sample.py limit=-10 -55 limit=10 55 $
0 コメント:
コメントを投稿