2012年8月5日日曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のV部(モジュール)のまとめ演習1(簡単なモジュールファイルのインポート)解いてみる。

1.

将来のPythonの機能を「オン」にできるけど、逆に過去のPythonの機能を「オン」することはできない。

コード(TextWrangler)

sample.py

#!/usr/bin/env python
#encoding: utf-8

def countLines(name):
 file = open(name)
 return len(file.readlines())

def countChars(name):
 file = open(name)
 return len(file.read())

def test():
 print("ファイル名を入力: ",end="")
 name = input()
 result = ("行数: " + str(countLines(name)) +  
   ", 文字数: " + str(countChars(name)))
 return result

入出力結果(Terminal)

$ python
Python 3.2.3 (default, Apr 18 2012, 20:17:30) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sample
>>> name = 'sample.py'
>>> sample.countLines(name)
17
>>> sample.countChars(name)
333
>>> sample.test()
ファイル名を入力: sample.py
'行数: 17, 文字数: 333'
>>> quit()
$

0 コメント:

コメントを投稿