作业 第三次

题目一

代码

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

运行结果

相关推荐
geovindu10 分钟前
rust: pool Pattern
开发语言·后端·设计模式·rust·对象池简写模式
坊钰14 分钟前
【LangGraph框架入门级】4. 支持联网搜索的智能代理系统
python·ai编程·langgraph
运维@小兵27 分钟前
安装Pycharm并破解
ide·python·pycharm
jimy133 分钟前
标准的动态类型定义与“多态”无关,非多态对象同样会出现动态类型不同于静态类型的情形
开发语言·c++
冬、不眠的夜33 分钟前
Nuitka 将 python 程序编译成 exe 指南
开发语言·python
CVer儿35 分钟前
起源1引擎相关知识点2--World Brush+func_detail
开发语言
范中勤1 小时前
Python Asyncio 协程与 LLM 异步并发原理技术文档(与 Java 对比|彻底澄清所有误区)
python·llm·协程·并发编程·asyncio
薛之谦_1 小时前
JAVA 多线程编程:从基础原理到实战应用
java·开发语言
Dmz丶1 小时前
前端转 AI 100 天 Day 21:FastAPI 入门——从命令行到 HTTP 服务
python
LMT_Lanmantech1 小时前
Creo许可降本增效:不增购,也能让现有许可发挥更大价值
java·大数据·开发语言