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))

@浙大疏锦行

相关推荐
xcLeigh15 分钟前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests
xcLeigh15 分钟前
Python入门:Python3 statistics模块全面学习教程
开发语言·python·学习·模块·python3·statistics
YongCheng_Liang36 分钟前
从零开始学 Python:自动化 / 运维开发实战(核心库 + 3 大实战场景)
python·自动化·运维开发
鸽芷咕1 小时前
为什么越来越多开发者转向 CANN 仓库中的 Python 自动化方案?
python·microsoft·自动化·cann
秋邱1 小时前
用 Python 写出 C++ 的性能?用CANN中PyPTO 算子开发硬核上手指南
开发语言·c++·python
wazmlp0018873692 小时前
python第三次作业
开发语言·python
深蓝电商API2 小时前
住宅代理与数据中心代理在爬虫中的选择
爬虫·python
历程里程碑3 小时前
普通数组----合并区间
java·数据结构·python·算法·leetcode·职场和发展·tornado
weixin_395448913 小时前
mult_yolov5_post_copy.c_cursor_0205
c语言·python·yolo
执风挽^3 小时前
Python基础编程题2
开发语言·python·算法·visual studio code