2013年4月2日火曜日

開発環境

初めてのコンピュータサイエンス(Jennifer CampbellPaul GriesJason MontojoGreg Wilson(著)長尾 高弘(翻訳))の2章(Python入門)の2.10の練習問題7、8、9、10を解いてみる。

7、8、9、10.

入出力結果(Terminal)

$ python
def cPython 3.3.0 (default, Sep 29 2012, 08:16:08) 
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def convert_mileage(mpg):
...     return 3.78541178/(mpg * 1.609455) * 100
... 
>>> convert_mileage(20)
11.759918046792237
>>> convert_mileage(40)
5.879959023396118
>>> def liters_needed(km, mpg):
...     return km * convert_mileage(mpg) / 100
... 
>>> # 上記の関数のkmやmpgが仮引数
... 
>>> # 次のように実際に渡す150, 30や100, 30が実引数
... 
>>> liters_needed(150, 30)
11.759918046792237
>>> liters_needed(100, 30)
7.839945364528157
>>> # convert_mileageの引数は30
... 
>>> # liters_needed(100, 30)という関数呼び出しはconvert_mileage関数呼び出しを引 き起こす
... 
>>> # 先に実行を終了するのはconvert_mileage関数
... 
>>> pow(3, 7)
2187
>>> int(34.7)
34
>>> round(34.7)
35
>>> float(abs(-86))
86.0
>>> quit()
$

0 コメント:

コメントを投稿