疏锦行Python打卡 DAY 26 函数专题1:函数定义与参数

import math

def calculate_circle_area(radius):
    if radius < 0:
        return 0 
    
    area = math.pi * (radius ** 2) 
    return area

print(f"半径 5 的面积: {calculate_circle_area(5)}")   
print(f"半径 0 的面积: {calculate_circle_area(0)}")   
print(f"半径 -1 的面积: {calculate_circle_area(-1)}") 


def calculate_rectangle_area(length, width):
    try:
        if length < 0 or width < 0:
            return 0
        return length * width
    except TypeError:
        return 0
    except:
        return 0

print(f"长度 5, 宽度 3 的面积: {calculate_rectangle_area(5, 3)}")   
print(f"长度 0, 宽度 0 的面积: {calculate_rectangle_area(0, 0)}")   
print(f"长度 -1, 宽度 2 的面积: {calculate_rectangle_area(-1, 2)}") 
print(f"长度 2, 宽度 -3 的面积: {calculate_rectangle_area(2, -3)}") 
print(f"长度 'a', 宽度 3 的面积: {calculate_rectangle_area('a', 3)}") 


def calculate_average(*args):
    if not args:
        return 0 
    
    try:
        if any(num < 0 for num in args):
            return 0  
        return sum(args) / len(args)
    
    except TypeError:
        return 0 

print(f"正常输入 (2, 3, 4): {calculate_average(2, 3, 4)}")         
print(f"空输入: {calculate_average()}")                            
print(f"负数输入 (-1, 2, 3): {calculate_average(-1, 2, 3)}")      
print(f"非数字输入 ('a', 2, 3): {calculate_average('a', 2, 3)}")  
print(f"浮点数输入 (2.5, 3.5, 4.5): {calculate_average(2.5, 3.5, 4.5)}")  

def print_user_info(user_id, **kwargs):
    print(f"User ID: {user_id}")  
    
    for key, value in kwargs.items():
        print(f"{key}: {value}")

print_user_info(1001, name="Alice", age=28, city="Shanghai")  

print_user_info("U12345", role="Admin", status="Active")  


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

desc1 = describe_shape("circle", radius=5, color="red")
print(desc1) 
desc2 = describe_shape("rectangle", length=10, width=4)
print(desc2)  

desc3 = describe_shape("triangle", base=6, height=8, color="blue")
print(desc3)  

desc4 = describe_shape("point", color="green")
print(desc4) 

打卡:@浙大疏锦行

Python中实现打卡兑换礼物的功能,通常会涉及到以下几个步骤: 1. **数据结构设计**:创建一个数据库或数据结构来存储用户的打卡记录,比如字典或列表,其中每个元素包含用户ID、日期等信息。 ```python users_gifts = {} # 使用字典,key为用户ID,value为打卡记录 ``` 2. **添加打卡功能**:编写函数,当用户调用时,检查用户是否存在并更新打卡次数。例如,可以使用`datetime`库来记录每日打卡时间。 ```python import datetime def check_in(user_id): today = datetime.datetime.now().strftime("%Y-%m-%d") if user_id not in users_gifts: users_gifts[user_id] = {today: 1} else: if today not in users_gifts[user_id]: users_gifts[user_id][today] = 1 else: users_gifts[user_id][today] += 1 ``` 3. **条件判断兑换规则**:设定一个规则,如连续7天打卡即可兑换一份礼物。可以遍历用户的打卡记录,检查是否符合条件。 ```python def can_exchange(user_id): user_history = users_gifts.get(user_id, {}) consecutive_days = {} for date, count in user_history.items(): if date - consecutive_days.get(date, '') <= datetime.timedelta(days=6): # 连续6天 consecutive_days[date] = count if len(consecutive_days) == 7: # 找到7连日 return True return False ``` 4. **兑换操作**:如果满足兑换条件,可以删除已达到兑换的打卡记录,并通知用户兑换成功。 ```python def redeem_gift(user_id): if can_exchange(user_id): for day, _ in list(users_gifts[user_id].items())[:7]: # 删除前7天的打卡记录 del users_gifts[user_id][day] print(f"恭喜用户{user_id},您的7天连续打卡已成功兑换礼物!") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值