作业 第三次

题目一

代码

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

运行结果

相关推荐
1024奇点2 小时前
【仓颉语言入门 · 第二篇 第 22 课】
开发语言·ide·开源
实名上网宋凯宣2 小时前
Django
后端·python·django
Warren2Lynch3 小时前
C4 模型详解:利用 Visual Paradigm 可视化软件架构的综合指南
开发语言·uml
如若1233 小时前
【论文复现】CoFailMed:从零复现医学图像分割的共享失败审计,含完整代码流程与科研绘图
人工智能·python
Knight_AL4 小时前
Java 文件下载时文件名中的 `#` 被转换成 `%23`?一次 URL 编码与解码问题排查
java·开发语言
FRing034 小时前
vue2干货总结2(v-bind、v-for、v-model、指令修饰符、v-bind操作class和style)
开发语言·前端·vue.js·vue2
by209995 小时前
C++ 智能指针的所有权与使用
开发语言·c++·经验分享
砚底藏山河5 小时前
python量化自定义周期 K 线合成
java·开发语言·数据库·python·金融
视觉小萌新5 小时前
AI Class 3 · Chapter 1:变量、字符串、list/dict、if/for(python学习-小白版-有经验可跳过)
python·学习·list
IT召唤狮5 小时前
开源企业级零信任项目Pangolin,零信任能挡住PHP核弹漏洞吗?
开发语言·开源·php