作业 第三次

题目一

代码

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

运行结果

相关推荐
Java后端的Ai之路8 小时前
17、Python - 观察者模式
开发语言·人工智能·python·观察者模式·外观模式
Metaphor6928 小时前
使用 Python 快速拆分 PDF 文件
python·pdf
阿童木写作8 小时前
自动智能抠图工具推荐:跨马翻译批量处理图片与视频字幕,跨境电商高效翻译
大数据·人工智能·python·音视频
OPEN-F8 小时前
C++并发编程:条件变量与原子操作
java·开发语言·c++
Kismet_nvi9 小时前
Python运维自动化核心模块
运维·python·自动化
神仙别闹9 小时前
基于 C++实现(控制台)车辆信息管理系统
开发语言·c++
智塑未来9 小时前
常用的在线ping测试工具有哪些
服务器·开发语言·php
卷无止境9 小时前
FastAPI 的可观测性进化,从打日志到看清整个系统的呼吸
后端·python·fastapi
Freak嵌入式9 小时前
MicroPython+Pico 接收空闲中断和发送空闲中断全实战
java·开发语言·stm32·单片机·嵌入式硬件
重生之小比特9 小时前
【初阶C++】string
开发语言·c++