作业 第三次

题目一

代码

python 复制代码
import os

def list_files(path):
    if os.path.isfile(path):
        print(path)
    elif os.path.isdir(path):
        for item in os.listdir(path):
            list_files(os.path.join(path, item))

运行结果

题目二

代码

python 复制代码
import hashlib
import os

USER_DB = 'users.txt'

def hash_password(password):
    return hashlib.sha256(password.encode()).hexdigest()

def register(username, password):
    with open(USER_DB, 'a') as f:
        f.write(f"{username}:{hash_password(password)}\n")
    print("注册成功")

def login(username, password):
    if not os.path.exists(USER_DB):
        return False
    with open(USER_DB, 'r') as f:
        for line in f:
            u, p = line.strip().split(':')
            if u == username and p == hash_password(password):
                return True
    return False

运行结果

题目三

代码

python 复制代码
class Student:
    def __init__(self, name, age, score):
        self.name = name
        self.age = age
        self.score = score
    
    def __repr__(self):
        return f"{self.name},{self.age},{self.score}"

class StudentSystem:
    FILE_NAME = 'students.txt'
    
    def add_student(self, student):
        with open(self.FILE_NAME, 'a', encoding='utf-8') as f:
            f.write(str(student) + '\n')
    
    def read_and_sort(self):
        students = []
        with open(self.FILE_NAME, 'r', encoding='utf-8') as f:
            for line in f:
                name, age, score = line.strip().split(',')
                students.append(Student(name, int(age), int(score)))
        
        students.sort(key=lambda s: s.score, reverse=True)
        return students

运行结果

相关推荐
zhangfeng11334 分钟前
openclaw skills 小龙虾技能 通讯仿真 matlab skill Simulink Agentic Toolkit,通过kimi找到,mcp通讯
开发语言·matlab·openclaw·通讯仿真
weixin_444012938 分钟前
PHP 中逻辑或(--)运算符的正确使用与条件逻辑重构指南
jvm·数据库·python
iAm_Ike6 小时前
Go 中自定义类型与基础类型间的显式类型转换详解
jvm·数据库·python
iuvtsrt6 小时前
Golang怎么实现方法集与接口的匹配_Golang如何理解值类型和指针类型实现接口的区别【详解】
jvm·数据库·python
chao1898447 小时前
基于 SPEA2 的多目标优化算法 MATLAB 实现
开发语言·算法·matlab
赏金术士7 小时前
Kotlin 习题集 · 高级篇
android·开发语言·kotlin
旦莫7 小时前
AI驱动的纯视觉自动化测试:知识库里应该积累什么知识内容
人工智能·python·测试开发·pytest·ai测试
楼兰公子8 小时前
buildroot 在编译rust时裁剪平台类型数量的方法
开发语言·后端·rust
知识领航员8 小时前
蘑兔AI音乐深度实测:功能拆解、实测表现与适用场景
java·c语言·c++·人工智能·python·算法·github
吴声子夜歌8 小时前
Go——并发编程
开发语言·后端·golang