開発環境
- 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章(関数と抽象化 - 関数にする)のエクササイズ(213ページ)の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3 from unittest import TestCase, main def make_sundae(ice_cream='vanilla', sauce='chocolate', nuts=True, banana=True, brownies=False, whipped_cream=True): recipe = f'{ice_cream} ice cream and {sauce} sauce ' if nuts: recipe += 'with nuts and ' if banana: recipe += 'a banana and ' if brownies: recipe += 'a brownie and ' if not whipped_cream: recipe += 'no ' recipe += 'whipped cream on top' return recipe class MyTestCase(TestCase): def setUp(self): pass def tearDown(self): pass def test(self): sundaes = [make_sundae(), make_sundae('chocolate'), make_sundae(sauce='caramel', whipped_cream=False, banana=False), make_sundae(whipped_cream=False, banana=True, brownies=True, ice_cream='peanut butter')] xs = ['vanilla ice cream and chocolate sauce with nuts and a banana ' + 'and whipped cream on top', 'chocolate ice cream and chocolate sauce with nuts and a banana' + ' and whipped cream on top', 'vanilla ice cream and caramel sauce with nuts and no whipped ' + 'cream on top', 'peanut butter ice cream and chocolate sauce with nuts and a ' + 'banana and a brownie and no whipped cream on top'] for sundae, x in zip(sundaes, xs): self.assertEqual(sundae, x) if __name__ == '__main__': main()
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))
C:\Users\...>py sample3.py . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK C:\Users\...>
0 コメント:
コメントを投稿