Python60日底子学习打卡D26

[复制链接]
发表于 2025-8-30 14:04:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
算圆形面积

错误代码 
  1. import math
  2. def calculate_circle_area(r):
  3.     try:
  4.         S = math.pi * r**2
  5.     except r<0:
  6.         print("半径不能为负数")
  7.     return S
复制代码
正确代码
  1. import math
  2. def calculate_circle_area(radius):
  3.     try:
  4.         if radius < 0:
  5.             return 0
  6.         return math.pi * radius ** 2
  7.     except Exception:
  8.         return 0
复制代码
 算矩形面积
  1. import math
  2. def calculate_rectangle_area(a,b):
  3.     try:
  4.         if a * b < 0:
  5.             return 0
  6.         return a * b
  7.     except Exception:
  8.         return 0
  9. print(calculate_rectangle_area(-5, 3))
复制代码
 求均匀数


  • 使命: 编写一个名为 calculate_average 的函数,该函数可以吸收任意数量的数字作为参数(引入可变位置参数 (*args)),并返回它们的均匀值。
  • 要求:利用 *args 来吸收全部传入的数字。       

    • 如果没有任何数字传入,函数应该返回 0。
    • 函数返回计算得到的均匀值。

  1. def calculate_average(*args):
  2.     if not args:
  3.         return 0
  4.     total = sum(args)
  5.     return total / len(args)
  6. print(calculate_average(1, 2, 3, 4, 5))
复制代码
打印用户信息

使命: 编写一个名为 print_user_info 的函数,该函数吸收一个必需的参数 user_id,以及任意数量的额外用户信息(作为关键字参数)。
要求:

  • user_id 是一个必需的位置参数。
  • 利用 **kwargs 来吸收任意数量的额外用户信息。
  • 函数打印出用户ID,然后逐行打印全部提供的额外信息(键和值)。
  • 函数不需要返回值
  1. def print_user_info(user_id, **kwargs):
  2.     print(f"用户ID: {user_id}")
  3.     for key, value in kwargs.items():
  4.         print(f"{key}: {value}")
  5. print(print_user_info(1, name="Alice", age=30, city="New York"))
复制代码
 不限数量的参数引用:
  1. // ... existing code ...
  2.     for key, value in kwargs.items():  # 遍历所有传入的关键字参数
  3.         print(f"{key}: {value}")      # 打印每个参数的键和值
  4. // ... existing code ...
复制代码
格式化多少图形描述

使命: 编写一个名为 describe_shape 的函数,该函数吸收图形的名称 shape_name (必需),一个可选的 color (默认 “black”),以及任意数量的描述该图形尺寸的关键字参数 (比方 radius=5 对于圆,length=10, width=4 对于矩形)。
要求:shape_name 是必需的位置参数。

  • color 是一个可选参数,默认值为 “black”。
  • 利用 **kwargs 网络描述尺寸的参数。
  • 函数返回一个描述字符串,格式如下:
  • “A [color] [shape_name] with dimensions: [dim1_name]=[dim1_value], [dim2_name]=[dim2_value], …”如果 **kwargs 为空,则尺寸部门为 “with no specific dimensions.”
错误代码 
  1. def describe_shape(shape_name,**kwargs):
  2.     print(f"A {shape_name} with dimensions")
  3.     for key, value in kwargs.items():
  4.         print(f"{key}: {value}")
  5. print(describe_shape(circle, age=30, city="New York"))
复制代码
正确代码
  1. def describe_shape(shape_name, color="black", **kwargs):
  2.     if kwargs:
  3.         dimensions = ', '.join([f"{key}={value}" for key, value in kwargs.items()])
  4.         return f"A {color} {shape_name} with dimensions: {dimensions}"
  5.     else:
  6.         return f"A {color} {shape_name} with no specific dimensions."
  7. def describe_shape(shape_name, color="black", **kwargs):
  8.     dimensions = ', '.join(f"{k}={v}" for k, v in kwargs.items())
  9.     dim_text = f"with dimensions: {dimensions}" if dimensions else "with no specific dimensions."
  10.     return f"A {color} {shape_name} {dim_text}"
复制代码
 
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表