作业 第三次

题目一

代码

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

运行结果

相关推荐
cuber膜拜1 小时前
Tenacity 原理与基本使用
服务器·网络·python·装饰器模式·tenacity
学编程的闹钟1 小时前
C语言WSAGetLastError函数
c语言·开发语言·学习
cuber膜拜1 小时前
PyBreaker 原理与基本使用
服务器·网络·python·pybreaker
学Linux的语莫1 小时前
模型转为RKNN格式
python·深度学习·机器学习
阿里嘎多学长1 小时前
2026-02-12 GitHub 热点项目精选
开发语言·程序员·github·代码托管
Albert Edison2 小时前
【Python】文件
android·服务器·python
沉睡的无敌雄狮2 小时前
可编程数字人落地实践:某省广电用矩阵跃动API重构工作流(选题→政策图谱→方言音色→审稿水印),附Python调度代码
人工智能·python·重构·排序算法·kmeans
Ronin3052 小时前
虚拟机数据管理模块
开发语言·c++·rabbitmq
3GPP仿真实验室2 小时前
【Matlab源码】6G候选波形:MIMO-OFDM-IM 增强仿真平台
开发语言·网络·matlab