開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 6(A Modular Approach to Program Organization)、Exercises 3の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3
import doctest
print('3.')
def average(num1: float, num2: float) -> float:
'''
>>> average(10, 20)
15.0
>>> average(2.5, 3.0)
2.75
'''
return (num1 + num2) / 2
if __name__ == '__main__':
doctest.testmod()
入出力結果(Zsh、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))
% mypy sample3.py
Success: no issues found in 1 source file
% ./sample3.py
3.
**********************************************************************
File "./sample3.py", line 9, in __main__.average
Failed example:
average(10, 20)
Expected:
15.0
Got:
20.0
**********************************************************************
File "./sample3.py", line 11, in __main__.average
Failed example:
average(2.5, 3.0)
Expected:
2.75
Got:
4.0
**********************************************************************
1 items had failures:
2 of 2 in __main__.average
***Test Failed*** 2 failures.
% mypy sample3.py
Success: no issues found in 1 source file
% ./sample3.py
3.
% ./sample3.py -v
3.
Trying:
average(10, 20)
Expecting:
15.0
ok
Trying:
average(2.5, 3.0)
Expecting:
2.75
ok
1 items had no tests:
__main__
1 items passed all tests:
2 tests in __main__.average
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
%
0 コメント:
コメントを投稿