Python60日基础学习打卡D26

算圆形面积

错误代码

python 复制代码
import math

def calculate_circle_area(r):
    try:
        S = math.pi * r**2
    except r<0:
        print("半径不能为负数")
    return S

正确代码

python 复制代码
import math

def calculate_circle_area(radius):
    try:
        if radius < 0:
            return 0
        return math.pi * radius ** 2
    except Exception:
        return 0

算矩形面积

python 复制代码
import math

def calculate_rectangle_area(a,b):
    try:
        if a * b < 0:
            return 0
        return a * b
    except Exception:
        return 0

print(calculate_rectangle_area(-5, 3)) 

求平均数

  • 任务: 编写一个名为 calculate_average 的函数,该函数可以接收任意数量的数字作为参数(引入可变位置参数 (*args) ),并返回它们的平均值。
  • 要求: 使用 *args 来接收所有传入的数字
    • 如果没有任何数字传入,函数应该返回 0。
    • 函数返回计算得到的平均值。
python 复制代码
def calculate_average(*args):
    if not args:
        return 0
    total = sum(args)
    return total / len(args)
print(calculate_average(1, 2, 3, 4, 5)) 

打印用户信息

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

要求:

  • user_id 是一个必需的位置参数。
  • 使用 **kwargs 来接收任意数量的额外用户信息。
  • 函数打印出用户ID,然后逐行打印所有提供的额外信息(键和值)。
  • 函数不需要返回值
python 复制代码
def print_user_info(user_id, **kwargs):
    print(f"用户ID: {user_id}")
    for key, value in kwargs.items():
        print(f"{key}: {value}")
print(print_user_info(1, name="Alice", age=30, city="New York"))

不限数量的参数引用:

python 复制代码
// ... existing code ...
    for key, value in kwargs.items():  # 遍历所有传入的关键字参数
        print(f"{key}: {value}")      # 打印每个参数的键和值
// ... 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."

错误代码

python 复制代码
def describe_shape(shape_name,**kwargs):
    print(f"A {shape_name} with dimensions")
    for key, value in kwargs.items():
        print(f"{key}: {value}")
print(describe_shape(circle, age=30, city="New York"))

正确代码

python 复制代码
def describe_shape(shape_name, color="black", **kwargs):
    if kwargs:
        dimensions = ', '.join([f"{key}={value}" for key, value in kwargs.items()])
        return f"A {color} {shape_name} with dimensions: {dimensions}"
    else:
        return f"A {color} {shape_name} with no specific dimensions."



def describe_shape(shape_name, color="black", **kwargs):
    dimensions = ', '.join(f"{k}={v}" for k, v in kwargs.items())
    dim_text = f"with dimensions: {dimensions}" if dimensions else "with no specific dimensions."
    return f"A {color} {shape_name} {dim_text}"
相关推荐
程序员三藏40 分钟前
Selenium无法定位元素的几种解决方案
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
前端小趴菜~时倾41 分钟前
自我提升-python爬虫学习:day04
爬虫·python·学习
小罗和阿泽42 分钟前
接口测试系列 接口自动化测试 pytest框架(三)
开发语言·python·pytest
毕设源码-邱学长7 小时前
【开题答辩全过程】以 基于Java的学校住宿管理系统的设计与实现为例,包含答辩的问题和答案
java·开发语言
rookieﻬ°8 小时前
PHP框架漏洞
开发语言·php
猿界零零七8 小时前
pip install mxnet 报错解决方案
python·pip·mxnet
炸膛坦客9 小时前
单片机/C/C++八股:(二十)指针常量和常量指针
c语言·开发语言·c++
兑生9 小时前
【灵神题单·贪心】1481. 不同整数的最少数目 | 频率排序贪心 | Java
java·开发语言
炸膛坦客10 小时前
单片机/C/C++八股:(十九)栈和堆的区别?
c语言·开发语言·c++
零雲10 小时前
java面试:了解抽象类与接口么?讲一讲它们的区别
java·开发语言·面试