開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
退屈なことはPythonにやらせよう ―ノンプログラマーにもできる自動化処理プログラミング (Al Sweigart(著)、相川 愛三(翻訳)、オライリージャパン)の第I部(Pythonプログラミングの基礎)、5章(辞書とデータ構造)、5.6(演習プロジェクト)、5.6.1(ファンタジーゲームの持ち物リスト)の解答を求めてみる。
コード
Python 3
sample1_test.py
#!/usr/bin/env python3
from unittest import TestCase, main
from test import support
from sample1 import display_inventory
class MyTestCase(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_display_inventory_output(self):
stuff = {'ロープ': 1, 'たいまつ': 6, '金貨': 42, '手裏剣': 1, '矢': 12}
expected = '''持ち物リスト:
1 ロープ
6 たいまつ
42 金貨
1 手裏剣
12 矢
アイテム総数: 62
'''
with support.captured_stdout() as stdout:
display_inventory(stuff)
self.assertEqual(expected, stdout.getvalue())
if __name__ == '__main__':
main()
sample1.py
#!/usr/bin/env python3
def display_inventory(stuff: dict) -> None:
item_total = 0
print('持ち物リスト:')
for k, v in stuff.items():
print(f'{v} {k}')
item_total += v
print(f'アイテム総数: {item_total}')
入出力結果(cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))
C:\Users\...>py -3 sample1_test.py -v test_display_inventory_output (__main__.MyTestCase) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.000s OK C:\Users\...>
0 コメント:
コメントを投稿