day30函数专题1:函数定义和参数@浙大疏锦行

day30函数专题1:函数定义和参数@浙大疏锦行

python 复制代码
import math
def calculate_circle_area(radius):
    if(radius < 0):
        raise ValueError("Radius cannot be negative")
    return math.pi * radius * radius

# print(calculate_circle_area(5))
# print(calculate_circle_area(0))
# print(calculate_circle_area(-3))  # This will raise an exception

# 计算矩形面积函数
def calculate_rectangle_area(length, width):
    if length < 0 or width < 0:
        return 0
    return length * width

# 测试用例
print(calculate_rectangle_area(5, 3))   # 输出: 15
print(calculate_rectangle_area(0, 10))  # 输出: 0
print(calculate_rectangle_area(-2, 4))  # 输出: 0
print(calculate_rectangle_area(6, -1))  # 输出: 0

# 计算平均值函数
def calculate_average(*args):
    if len(args) == 0:
        return 0
    return sum(args) / len(args)

# # 测试用例
# print(calculate_average(1, 2, 3, 4, 5))  # 输出: 3.0
# print(calculate_average(10, 20))         # 输出: 15.0
# print(calculate_average())               # 输出: 0
# print(calculate_average(7))              # 输出: 7.0

# 打印用户信息函数
def print_user_info(user_id, **kwargs):
    print(f"用户ID: {user_id}")
    for key, value in kwargs.items():
        print(f"{key}: {value}")

# # 测试用例
# print_user_info(1001, name="张三", age=20, city="北京")
# print_user_info(1002)
# print_user_info(1003, email="test@example.com")

# 图形描述函数
def describe_shape(shape_name, color="black", **kwargs):
    desc = f"A {color} {shape_name}"
    if kwargs:
        dims = ", ".join([f"{k}={v}" for k, v in kwargs.items()])
        desc += f" with dimensions: {dims}."
    else:
        desc += " with no specific dimensions."
    return desc

# # 测试用例
# print(describe_shape("circle", radius=5))
# print(describe_shape("rectangle", "red", length=10, width=4))
# print(describe_shape("triangle"))
# print(describe_shape("ellipse", color="blue", a=3, b=2))

@浙大疏锦行

相关推荐
Buxxxxxx1 小时前
DAY 34 模块和库的导入
开发语言·python
haiyu_y1 小时前
Day 27 通用机器学习流水线
人工智能·python·机器学习
CHANG_THE_WORLD1 小时前
“元组“名称的由来
python
Silence_Jy1 小时前
deepseek-R1技术报告解析
python·深度学习·transformer
Eric.Lee20212 小时前
mujoco读取模型几何体属性
python·物理引擎·mujoco·物理仿真·构建物理几何体
子午2 小时前
【车辆车型识别系统】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积网络+resnet50算法
人工智能·python·深度学习
独行soc2 小时前
2025年渗透测试面试题总结-273(题目+回答)
网络·python·安全·web安全·网络安全·渗透测试·安全狮
独行soc2 小时前
2025年渗透测试面试题总结-274(题目+回答)
网络·python·安全·web安全·网络安全·渗透测试·安全狮
CHANG_THE_WORLD2 小时前
Python列表(List)介绍
windows·python·list