開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Head First はじめてのプログラミング ―頭とからだで覚えるPythonプログラミング入門 (Eric Freeman(著)、嶋田 健志(監修)、木下 哲也(翻訳)、株式会社オライリー・ジャパン)を5章(関数と抽象化 - 関数にする)のコードマグネット(191ページ)の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3
from unittest import TestCase, main
from test.support import captured_stdout
def how_should_i_get_threre(miles):
if miles > 120:
print('Take a plane')
elif miles >= 2.0:
print('Take a car')
else:
print('Walk')
class MyTestCase(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test(self):
with captured_stdout() as stdout:
how_should_i_get_threre(800.3)
how_should_i_get_threre(2.0)
how_should_i_get_threre(.5)
output = '''Take a plane
Take a car
Walk
'''
self.assertEqual(stdout.getvalue(), output)
if __name__ == '__main__':
main()
入出力結果(Bash、cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))
C:\Users\...>py sample1.py . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK C:\Users\...>
0 コメント:
コメントを投稿