作业 第三次

题目一

代码

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

运行结果

相关推荐
杰克尼3 分钟前
天机学堂项目总结(day3~day4)
java·开发语言·spring
emovie3 分钟前
Python函数基础
linux·数据库·python
我叫Ycg5 分钟前
C++ 中关于插入函数insert() 与 emplace() 的区别与使用建议
开发语言·c++
Polar__Star6 分钟前
SQL中JOIN不同存储引擎表的影响_索引兼容性与查询性能评估
jvm·数据库·python
2301_815279526 分钟前
golang如何实现数据导入进度跟踪_golang数据导入进度跟踪实现教程
jvm·数据库·python
hsg776 分钟前
简述:pytorch
人工智能·pytorch·python
YuanDaima20486 分钟前
矩阵基础原理与题目说明
人工智能·笔记·python·学习·线性代数·矩阵
环小保7 分钟前
NMP回收设备厂家深度解析:锂电绿色制造的核心力量
python·制造
码农的神经元12 分钟前
2026 MathorCup 选题建议:A/B/C/D/E 题到底怎么选?
c语言·开发语言·数学建模
InfinteJustice14 分钟前
golang如何使用struct嵌套_golang struct结构体嵌套使用方法.txt
jvm·数据库·python