Files
project_test/mymath.py
User 5033b5f97e Initial commit
- Add mymath.py with multiply and divide functions
- Add SmartVoyage travel assistant project
- Add .gitignore for Python cache files

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-02 15:05:06 +08:00

72 lines
1.7 KiB
Python

def multiply(a, b):
"""
计算两个数字的乘积
Args:
a: 第一个数字
b: 第二个数字
Returns:
两个数字的乘积
"""
return a * b
def divide(a, b):
"""
计算两个数字的商
Args:
a: 被除数
b: 除数
Returns:
两个数字的商
Raises:
ValueError: 如果除数为零
"""
if b == 0:
raise ValueError("除数不能为零")
return a / b
if __name__ == "__main__":
# 测试乘法函数
print("测试乘法函数:")
test_cases_multiply = [
(2, 3, 6),
(-2, 3, -6),
(0, 5, 0),
(2.5, 4, 10.0),
(-2.5, -2, 5.0)
]
for a, b, expected in test_cases_multiply:
result = multiply(a, b)
status = "PASS" if result == expected else "FAIL"
print(f" {status} multiply({a}, {b}) = {result} (期望: {expected})")
# 测试除法函数
print("\n测试除法函数:")
test_cases_divide = [
(6, 3, 2),
(10, 2, 5),
(-6, 2, -3),
(5, 2, 2.5),
(0, 5, 0)
]
for a, b, expected in test_cases_divide:
try:
result = divide(a, b)
status = "PASS" if result == expected else "FAIL"
print(f" {status} divide({a}, {b}) = {result} (期望: {expected})")
except ValueError as e:
print(f" FAIL divide({a}, {b}) 出错: {e}")
# 测试除数为零的情况
print("\n测试除数为零:")
try:
divide(5, 0)
print(" FAIL divide(5, 0) 应该抛出异常但没有")
except ValueError as e:
print(f" PASS divide(5, 0) 正确抛出异常: {e}")