Alright on Monday I wasn't able to get on due to computer issues. This continued to at least half of the class on Tuesday. So today i'll continue on Chapter 5 questions #1.
#1:
def compare(a, b)
"""
>>> compare(5, 4)
1
>>> compare(7, 7)
0
>>> compare(2, 3)
-1
>>> compare(42, 1)
1
"""
"""
>>>compare(a > b)
1
>>>compare(a == b)
0
>>>compare(a < b)
-1
"""
if __name__== ' __main__':
import doctest
doctest.testmod()
This program really got me for awhile. When I first started out I had changed the def compare(a, b) to def compare( a > b). Which was giving me an error saying that > was an invalid syntax. But the spur of the moment thought make me think to instead add a > b, a == b, and a < b, to next to the >>>compare. After one try i got this right away. This was a pretty easy program to make after I found out exactly what I had to do.
#2: def hypotenuse(a, b): """ >>> hypotenuse(3, 4) 5.0 >>> hypotenuse(12, 5) 13.0 >>> hypotenuse(7, 24) 25.0 >>> hypotenuse(9, 12) 15.0 """ if __name__== '__main__': import doctest doctest.testmod()
Which I then got an output of:
*****************************************************************************
File "hypotenuse.py", line 7, in __main__.hypotenuse
Failed example:
hypotenuse(7, 24)
Expected:
25.0
Got nothing
*****************************************************************************
File "hypotenuse.py", line 9, in __main__.hypotenuse
Failed example:
hypotenuse(9, 12)
Expected:
15.0
Got nothing
*****************************************************************************
1 items had failures:
4 of 4 in __ main__.hypotenuse
***Test Failed*** 4 failures.
#3: def slope(x1, y1, x2, y2): """ >>> slope(5, 3, 4, 2) 1.0 >>> slope(1, 2, 3, 2) 0.0 >>> slope(1, 2, 3, 3) 0.5 >>> slope(2, 4, 1, 2) 2.0 """ if __name__== '__main__': import doctest doctest.testmod()
With an end result of:
*****************************************************************************
File "slope.py", line 7, in __main__.slope
Failed example:
slope(1, 2, 3, 3)
Expected:
0.5
Got nothing
*****************************************************************************
File "slope.py", line 9, in __main__.slope
Failed example:
slope(2, 4, 1, 2)
Expected:
2.0
Got nothing
*****************************************************************************
1 items had failures:
4 of 4 in __main__.slope
***Test Failed*** 4 failures.
ef intercept(x1, y1, x2, y2):
"""
>>> intercept(1, 6, 3, 12)
3.0
>>> intercept(6, 1, 1, 6)
7.0
>>> intercept(4, 6, 12, 8)
5.0
"""
if __name__== '__main__':
import doctest
doctest.testmod()
With a end result of:
*****************************************************************************
Ill have continue this tomorrow.