2018年5月3日木曜日

開発環境

入門 Python 3 (Bill Lubanovic (著)、斎藤 康毅 (監修)、長尾 高弘 (翻訳)、オライリージャパン)の7章(プロのようにデータを操る)、7.3(復習問題)4、5、6.を取り組んでみる。

コード(Emacs)

Python 3

#!/usr/bin/env python3

print('7-4')
a = '''My kitty cat likes %s,
My kitty cat likes %s,
My kitty cat fell on his %s
And now thinks he's a %s.'''
print(a % ('roast beef', 'ham', 'head', 'clam'))

print('7-5')
letter = '''Dear {salutation} {name},

Tank you for your letter. We are sorry that our {product} {verbed} in your
{room}. Please note that it should never be used in a {room}, especially
near any {animals}.

Send us your receipt and {amount} for shipping and handling. We will send
you another {product} that, in our tests, is {percent}% less likely to
have {verbed}.

Tnak you for your support.

Sincerely,
{spokesman}
{job_title}'''

print('7-6')
response = dict(salutation='SALUTATION',
                name='NAME',
                product='PRODUCT',
                verbed='VERBED',
                room='ROOM',
                animals='ANIMALS',
                amount='AMOUNT',
                percent='PERCENT',
                spokesman='SPOKESMAN',
                job_title='JOB_TITLE')
print(response)
print(letter.format(**response))

入出力結果(Terminal, Jupyter(IPython))

$ ./sample2.py
7-4
My kitty cat likes roast beef,
My kitty cat likes ham,
My kitty cat fell on his head
And now thinks he's a clam.
7-5
7-6
{'salutation': 'SALUTATION', 'name': 'NAME', 'product': 'PRODUCT', 'verbed': 'VERBED', 'room': 'ROOM', 'animals': 'ANIMALS', 'amount': 'AMOUNT', 'percent': 'PERCENT', 'spokesman': 'SPOKESMAN', 'job_title': 'JOB_TITLE'}
Dear SALUTATION NAME,

Tank you for your letter. We are sorry that our PRODUCT VERBED in your
ROOM. Please note that it should never be used in a ROOM, especially
near any ANIMALS.

Send us your receipt and AMOUNT for shipping and handling. We will send
you another PRODUCT that, in our tests, is PERCENT% less likely to
have VERBED.

Tnak you for your support.

Sincerely,
SPOKESMAN
JOB_TITLE
$

0 コメント:

コメントを投稿