news 2026/5/1 11:14:39

第三次Python练习题

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
第三次Python练习题

1.使用os和os.path以及函数的递归完成:给出一个路径,遍历当前路径所有的文件及文件夹打印输出所有的文件(遇到文件输出路径,遇到文件夹继续进文件夹)

import os import os.path def list_all_file(path): for name in os.listdir(path): #逐个遍历这个名称列表,每次循环拿到一个文件 / 文件夹的名称 name。 file_path = os.path.join(path,name) #把当前路径 path 和名称 name 拼接成完整的文件 / 文件夹路径。 if os.path.isfile(file_path):#判断 file_path 是否是一个文件。 print(file_path) elif os.path.isdir(file_path): #判断 file_path 是否是一个文件夹。 list_all_file(file_path) if __name__ == "__main__": list_all_file("D:\\Python\\code\\05.Python-io")

2.使用加密模块及IO模拟登录功能,要求使用文件模拟数据库存储用户名和密码。

import hmac datebase = {} def encryption_admin(str): salt = "%%$$&&".encode("utf-8") return hmac.new(str.encode("utf-8"),salt,"md5").hexdigest() datebase["username"] = encryption_admin("zhangsan") datebase["password"] = encryption_admin("111") username = input("请输入用户名:") password = input("请输入密码:") if (encryption_admin(username) == datebase["username"]) and (encryption_admin(password) == datebase["password"]): print("login success") else: print("login failure")

3.使用面向对象编程完成学生信息录入功能,数据存储在本地文件txt中并读取学生信息并按照成绩进行排序,学生其他属性自行规划

class Student: """学生类""" def __init__(self, name, age, score): self.name = name self.age = int(age) self.score = float(score) def __str__(self): """打印信息""" return f"姓名:{self.name}, 年龄:{self.age}, 成绩:{self.score}" def __lt__(self, other): return self.score < other.score def __eq__(self, other): return self.name == other.name and self.age == other.age and self.score == other.score def __hash__(self): return hash((self.name, self.age, self.score)) def save_student(name, age, score): """保存学生信息""" try: with open("D:\\Python\\code\\05.Python-io\\student.txt", "a", encoding="utf-8") as f: f.write(f"{name},{age},{score}\n") print("保存成功") except Exception as e: print("异常信息:", e) def read_student(): """读取学生信息""" students = set() try: with open("D:\\Python\\code\\05.Python-io\\student.txt", "r", encoding="utf-8") as f: for line in f: name, age, score = line.strip().split(",") students.add(Student(name, age, score)) except Exception as e: print("异常信息:", e) return list(students) # 将集合转换为列表 def input_student(): """录入学生信息""" students = [] while True: name = input("请输入学生姓名(输入q结束):") if name == "q": break age = input("请输入学生年龄:") score = input("请输入学生成绩:") student = Student(name, age, score) students.append(student) save_student(name, age, score) return students def sort_student(students): """排序""" students.sort(reverse=True) return students if __name__ == "__main__": # 录入学生信息 students = input_student() # 保存学生信息 for student in students: save_student(student.name, student.age, student.score) # 读取学生信息 students = read_student() # 排序学生信息 sorted_students = sort_student(students) # 打印排序后的学生信息 print("排序后的学生信息:") for s in sorted_students: print(s)

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/30 20:43:53

使用 Depth Anything V2 进行单目深度估计

原文&#xff1a;towardsdatascience.com/monocular-depth-estimation-with-depth-anything-v2-54b6775abc9f?sourcecollection_archive---------4-----------------------#2024-07-24 神经网络是如何从二维图像中学习估计深度的&#xff1f; https://medium.com/neural.avb?…

作者头像 李华
网站建设 2026/5/1 5:04:30

什么是住宅代理IP?

什么是住宅代理IP&#xff1f; 住宅代理IP是一种特殊类型的代理服务&#xff0c;采用的IP地址为居民住宅网络IP地址。这种特殊类型的代理服务可以模拟真实用户的上网行为和位置信息&#xff0c;从而更好地保护用户的隐私&#xff0c;并且比其他类型的代理服务更难被网站或应用…

作者头像 李华
网站建设 2026/5/1 8:34:41

Ps:清晰度和去除薄雾

清晰度和去除薄雾 Clarity and Dehaze是 Photoshop 中源自 Camera Raw 的核心影像结构调整算法&#xff0c;在 Ps 里以调整图层的形式出现&#xff0c;使用户能够在不破坏原图的前提下&#xff0c;对画面结构对比和空气感进行精确控制。Ps菜单&#xff1a;图层 / 新建调整图层 …

作者头像 李华
网站建设 2026/5/1 5:03:01

单声道到立体声:AI 如何为音乐注入新生命

原文&#xff1a;towardsdatascience.com/mono-to-stereo-how-ai-is-breathing-new-life-into-music-4180f1357db4?sourcecollection_archive---------4-----------------------#2024-12-24 AI 单声道到立体声升混的应用与技术 https://medium.com/maxhilsdorf?sourcepost_p…

作者头像 李华
网站建设 2026/4/30 18:49:35

Qwen3-VL-Reranker-8B应用场景:医疗影像报告图文混合语义检索系统

Qwen3-VL-Reranker-8B应用场景&#xff1a;医疗影像报告图文混合语义检索系统 1. 这不是普通“搜图”&#xff0c;而是让医生秒懂影像与报告的关联 你有没有遇到过这样的场景&#xff1a;一位放射科医生在查阅某位肺癌患者的CT影像时&#xff0c;想快速找到过去三年内所有相似…

作者头像 李华