2019年4月10日水曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 2(Hello, Python)、Exercises 4、5、6、7、8の解答を求めてみる。

コード

Python 3

#!/usr/bin/env python3
from unittest import TestCase, main


class MyTestCase(TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test4(self):
        expected = [46, 5.75, -157]
        actual = [6 * 3 + 7 * 4,
                  5 + 3 / 4,
                  5 - 2 * 3 ** 4]
        for e, a in zip(expected, actual):
            self.assertEqual(e, a)

    def test5(self):
        x = 10.5
        y = 4
        x = x + y
        self.assertEqual(x, 14.5)
        self.assertEqual(y, 4)

    def test6(self):
        x = 3
        x += x - 3
        # x - 3 = 0
        # x = x + 0
        # x = 3 + 0
        # x = 3
        self.assertEqual(x, 3)

    def test7(self):
        with self.assertRaises(NameError):
            print(a)

    # 8.
    # 8 = people がSyntaxError
    # 4 += 7 / 2
    # 他は問題ない


if __name__ == '__main__':
    main()

入出力結果(cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))

C:\Users\...>py sample4.py -v
test4 (__main__.MyTestCase) ... ok
test5 (__main__.MyTestCase) ... ok
test6 (__main__.MyTestCase) ... ok
test7 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK

C:\Users\...>

0 コメント:

コメントを投稿