import math
#print(math.sqrt(2))
assert abs(math.sqrt(2) - 1.41421) < 0.001, "math.sqrt works"
def checkWithin(result: float, expected: float, tolerance: float, name: str) -> None:
assert abs(result - expected) < tolerance, name
checkWithin(math.sqrt(2), 1.51421, 0.001, "math.sqrt works")
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) Input In [36], in <cell line: 4>() 1 import math 2 #print(math.sqrt(2)) ----> 4 assert abs(math.sqrt(2) - 1.51421) < 0.001 6 def checkWithin(result: float, expected: float, tolerance: float, name: str) -> None: 7 assert abs(result - expected) < tolerance, name AssertionError:
def hypotenuse(a: float, b: float) -> float:
return math.sqrt(a**2 + b**2)
print(hypotenuse(4, 5) > 7)
False
def pos(x: float) -> float:
"""
If x is not zero, return the absolute value of x. Otherwise, return 1.
"""
if x < 0:
return -x
elif x == 0:
return 1
else:
return x
print(pos(42))
42
def multiOp(x: float, y: float, op: int) -> float:
"""
Return "x op y", where the operation is given as a code in "op":
0: addition
1: subtraction
2: multiplication
"""
print("A")
assert op >= 0, "No negative operation codes"
print("B")
assert op <= 2, "Operation codes go up to 2"
print("C")
if op == 0:
print("D")
return x + y
print("E")
if op == 1:
print("F")
return x - y
print("G")
return x * y
print("H")
multiOp(1, 2, 2)
A B C E G
2
x = hypotenuse(4, 5) < 7
if x:
print("Hello!")
Hello!
def multiOp(x: float, y: float, op: int) -> float:
"""
Return "x op y", where the operation is given as a code in "op":
0: addition
1: subtraction
2: multiplication
"""
assert op >= 0, "No negative operation codes"
assert op <= 2, "Operation codes go up to 2"
print("A")
if op < 2:
print("B")
# addition or subtraction
if op == 0:
print("C")
return x + y
else:
print("D")
return x - y
print("E")
else:
print("F")
return x * y
multiOp(1, 2, 2)
A F
2
def multiOp(x: float, y: float, op: int) -> float:
"""
Return "x op y", where the operation is given as a code in "op":
0: addition
1: subtraction
2: multiplication
"""
if op >= 0 and op <= 2:
if op == 0:
return x + y
if op == 1:
return x - y
return x * y
else: # op out of range
return -1
multiOp(1, 2, 2)
def multiOp(x: float, y: float, op: int) -> float:
"""
Return "x op y", where the operation is given as a code in "op":
0: addition
1: subtraction
2: multiplication
"""
print("A")
if opInRange(op):
print("B")
if op == 0:
return x + y
#print(x + y)
if x == 42:
return x - y
return x * y
else: # op out of range
print("C")
return -1
def opInRange(op: int) -> bool:
print("D")
return op >= 0 and op <= 2
multiOp(1, 2, -1)
print(opInRange(-1))
<cell>22: error: Name "op" is not defined [name-defined]
A D
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [35], in <cell line: 24>() 21 print("D") 22 return op >= 0 and op <= 2 ---> 24 multiOp(1, 2, -1) 25 print(opInRange(-1)) Input In [35], in multiOp(x, y, op) 2 """ 3 Return "x op y", where the operation is given as a code in "op": 4 0: addition 5 1: subtraction 6 2: multiplication 7 """ 8 print("A") ----> 9 if opInRange(op): 10 print("B") 11 if op == 0: Input In [35], in opInRange(puppy) 20 def opInRange(puppy: int) -> bool: 21 print("D") ---> 22 return op >= 0 and op <= 2 NameError: name 'op' is not defined