開発環境
- 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 (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の4章(複数のソースファイルの使用: 分割して構築する)、エクササイズ(p.165)をpythonで考えてみる。
エクササイズ(p.165)
コード(BBEdit, Emacs)
sample165.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
total = 0
count = 0
tax_percent = 6
def add_with_tax(f):
tax_rate = 1 + tax_percent / 100
global total
total += f * tax_rate
global count
count += 1
return total
val = input('品目の値段:')
while val:
val = float(val)
print('ここまでの合計:{0:.2f}'.format(add_with_tax(val)))
val = input('品目の値段:')
print('最終合計:{0:.2f}'.format(total))
print('品目数:{0}'.format(count))
入出力結果(Terminal, IPython)
$ ./sample165.py 品目の値段:12.34 ここまでの合計:13.08 品目の値段:56.78 ここまでの合計:73.27 品目の値段:90.12 ここまでの合計:168.79 品目の値段:12345.67 ここまでの合計:13255.20 品目の値段:1.2 ここまでの合計:13256.48 品目の値段: 最終合計:13256.48 品目数:5 $
0 コメント:
コメントを投稿