開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の7章(高度な関数: 関数を最大限に活用する)、プールパズル(p.335)をpythonで考えてみる。
プールパズル(p.335)
コード(BBEdit, Emacs)
sample328.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import enum
response_type = enum.Enum('Response', 'dump second_chance marriage')
class Response:
def __init__(self, name, res_type):
self.name = name
self.res_type = res_type
def dump(self):
print('{0}さんへ'.format(self.name))
print('残念ながら、前回のデートの結果、')
print('再度お会いすることはないとの連絡を受けました。')
def secondChance(self):
print('{0}さんへ'.format(self.name))
print('よいお知らせです。前回のデートの結果、')
print('もう一度お会いしたいとの連絡を受けました。至急ご連絡ください。')
def marriage(self):
print('{0}さんへ'.format(self.name))
print('おめでとうございます!前回のデートの結果、')
print('結婚を申し込みたいとの連絡を受けました。')
r = [Response('マイク', response_type.dump),
Response('ルイス', response_type.second_chance),
Response('マット', response_type.second_chance),
Response('ウィリアム', response_type.marriage)]
for res in r:
if res.res_type == response_type.dump:
res.dump()
elif res.res_type == response_type.second_chance:
res.secondChance()
else:
res.marriage()
入出力結果(Terminal, IPython)
$ ./sample335.py マイクさんへ 残念ながら、前回のデートの結果、 再度お会いすることはないとの連絡を受けました。 ルイスさんへ よいお知らせです。前回のデートの結果、 もう一度お会いしたいとの連絡を受けました。至急ご連絡ください。 マットさんへ よいお知らせです。前回のデートの結果、 もう一度お会いしたいとの連絡を受けました。至急ご連絡ください。 ウィリアムさんへ おめでとうございます!前回のデートの結果、 結婚を申し込みたいとの連絡を受けました。 $
0 コメント:
コメントを投稿