Python打卡 DAY 28

知识点回顾:

1.  类的定义

2.  pass占位语句

3.  类的初始化方法

4.  类的普通方法

5.  类的继承:属性的继承、方法的继承

 

题目1:定义圆(Circle)类
要求:
1.包含属性:半径 radius。
2.包含方法:
●calculate_area():计算圆的面积(公式:πr²)。
●calculate_circumference():计算圆的周长(公式:2πr)。
3.初始化时需传入半径,默认值为 1。 

import math
 
class Circle:
    def __init__(self, radius=1):
        self.radius = radius # 外界的参数,需要通过self.xxx来复制给类自己的属性
 
    def calculate_area(self):
        return math.pi * (self.radius ** 2)
 
    def calculate_circumference(self):
        return 2 * math.pi * self.radius

circle = Circle(5)
print("Area:", circle.calculate_area())
print("Circumference:", circle.calculate_circumference())

题目2:定义长方形(Rectangle)类
1.包含属性:长 length、宽 width。
2.包含方法:
●calculate_area():计算面积(公式:长×宽)。
●calculate_perimeter():计算周长(公式:2×(长+宽))。 is_square() 方法,判断是否为正方形(长 == 宽)。
3.初始化时需传入长和宽,默认值均为 1。 

class Rectangle:
    def __init__(self, length=1, width=1):
        self.length = length # 外界的参数,需要通过self.xxx来复制给类自己的属性
        self.width = width
    
    def calculate_area(self):
        return self.length * self.width
 
    def calculate_circumference(self):
        return 2 * (self.length  + self.width)
 
    def is_square(self):
        return self.length == self.width

rectangle = Rectangle(5,5)
print("Area:", rectangle.calculate_area())
print("Circumference:", rectangle.calculate_circumference())

题目3:图形工厂
创建一个工厂函数 create_shape(shape_type, *args),根据类型创建不同图形对象:图形工厂(函数或类)
shape_type=“circle”:创建圆(参数:半径)。
shape_type=“rectangle”:创建长方形(参数:长、宽)。 


def create_shape(shape_type, *args):
    if shape_type == "circle":
        if len(args) != 1: #如果 shape_type 是 "rectangle",则检查 args 是否有两个元素(即长度和宽度)。
            raise ValueError("Circle requires exactly one argument: radius.") #如果参数数量不正确,抛出异常。
        return Circle(*args) 
    elif shape_type == "rectangle": #否则,使用传入的长度和宽度创建并返回一个 Rectangle 对象
        if len(args) != 2:
            raise ValueError("Rectangle requires exactly two arguments: length and width.")
        return Rectangle(*args)
    else:
        raise ValueError(f"Unknown shape type: {shape_type}") #如果形状类型未知,抛出异常。

circle = create_shape("circle", 5)
print("Circle Area:", circle.calculate_area())
print("Circle Circumference:", circle.calculate_circumference())

@浙大疏锦行

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、付费专栏及课程。

余额充值