Files
han/mymath.py

34 lines
796 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
def multiply(a, b):
"""返回两个数字的乘积"""
return a * b
def divide(a, b):
"""返回两个数字的商b不能为零"""
if b == 0:
raise ValueError("除数不能为零")
return a / b
if __name__ == "__main__":
# 测试乘法
assert multiply(3, 4) == 12
assert multiply(-2, 5) == -10
assert multiply(0, 100) == 0
print("乘法测试通过")
# 测试除法
assert divide(10, 2) == 5
assert divide(9, 3) == 3
assert divide(5, 2) == 2.5
print("除法测试通过")
# 测试除零异常
try:
divide(1, 0)
print("ERROR: 除零异常未触发")
except ValueError as e:
assert str(e) == "除数不能为零"
print("除零异常测试通过")
print("所有测试通过")