项目一猜数字游戏项目描述创建一个简单的猜数字游戏程序随机生成一个1-100之间的数字玩家需要猜出这个数字程序会根据猜测结果给出提示。功能要求● 随机生成1-100之间的目标数字● 接受用户输入的猜测数字● 判断猜测结果并给出提示太大、太小或正确● 统计猜测次数● 支持重新开始游戏代码实现import random def guess_number_game(): 猜数字游戏主函数 print( 猜数字游戏 ) print(我想了一个1到100之间的数字你能猜出来吗) # 生成随机数 target_number random.randint(1, 100) guess_count 0 while True: try: # 获取用户输入 user_input input(\n请输入你的猜测输入q退出游戏) # 退出游戏 if user_input.lower() q: print(f游戏结束正确答案是 {target_number}) break # 转换为整数 guess int(user_input) guess_count 1 # 判断猜测结果 if guess target_number: print(太小了再试试) elif guess target_number: print(太大了再试试) else: print(f恭喜你猜对了) print(f你总共猜了 {guess_count} 次) break except ValueError: print(请输入一个有效的数字) # 询问是否重新开始 play_again input(\n是否想再玩一次(y/n): ) if play_again.lower() y: guess_number_game() # 运行游戏 if __name__ __main__: guess_number_game()项目二简易命令行计算器项目描述开发一个支持基本四则运算的命令行计算器用户可以输入表达式进行计算。功能要求● 支持加、减、乘、除运算● 处理用户输入错误● 支持连续计算● 提供简单的用户界面代码实现def calculator(): 简易命令行计算器 print( 简易计算器 ) print(支持的运算、-、*、/) print(输入 quit 退出程序) while True: # 获取用户输入 expression input(\n请输入计算表达式如2 3).strip() # 退出程序 if expression.lower() quit: print(计算器已关闭) break # 解析表达式 try: parts expression.split() if len(parts) ! 3: print(请输入正确的格式数字 运算符 数字) continue num1 float(parts[0]) operator parts[1] num2 float(parts[2]) # 执行计算 if operator : result num1 num2 elif operator -: result num1 - num2 elif operator *: result num1 * num2 elif operator /: if num2 0: print(错误除数不能为零) continue result num1 / num2 else: print(不支持的运算符请使用 、-、*、/) continue print(f结果{num1} {operator} {num2} {result}) except ValueError: print(请输入有效的数字) except Exception as e: print(f计算错误{e}) # 运行计算器 if __name__ __main__: calculator()项目三待办事项清单项目描述创建一个简单的待办事项管理程序支持添加、查看、删除和保存待办事项数据使用JSON文件存储。功能要求● 添加新的待办事项● 查看所有待办事项● 删除已完成的事项● 将数据保存到JSON文件● 从文件加载数据代码实现图import json import os class TodoList: 待办事项清单类 def __init__(self, filenametodo_list.json): self.filename filename self.tasks self.load_tasks() def load_tasks(self): 从文件加载任务 if os.path.exists(self.filename): try: with open(self.filename, r, encodingutf-8) as f: return json.load(f) except json.JSONDecodeError: print(警告任务文件损坏将创建新的任务列表) return [] else: return [] def save_tasks(self): 保存任务到文件 with open(self.filename, w, encodingutf-8) as f: json.dump(self.tasks, f, ensure_asciiFalse, indent2) def add_task(self, task): 添加任务 self.tasks.append({ task: task, completed: False }) self.save_tasks() print(f已添加任务{task}) def view_tasks(self): 查看所有任务 if not self.tasks: print(暂无待办事项) return print(\n 待办事项清单 ) for i, task in enumerate(self.tasks): status ✓ if task[completed] else ○ print(f{i1}. [{status}] {task[task]}) def complete_task(self, task_index): 标记任务为完成 if 0 task_index len(self.tasks): self.tasks[task_index][completed] True self.save_tasks() print(f已标记为完成{self.tasks[task_index][task]}) else: print(无效的任务编号) def delete_task(self, task_index): 删除任务 if 0 task_index len(self.tasks): removed_task self.tasks.pop(task_index) self.save_tasks() print(f已删除任务{removed_task[task]}) else: print(无效的任务编号) def run(self): 运行待办事项程序 print( 待办事项清单 ) while True: print(\n--- 操作菜单 ---) print(1. 查看所有任务) print(2. 添加新任务) print(3. 标记任务为完成) print(4. 删除任务) print(5. 退出程序) choice input(\n请选择操作1-5) if choice 1: self.view_tasks() elif choice 2: task input(请输入新任务).strip() if task: self.add_task(task) else: print(任务内容不能为空) elif choice 3: self.view_tasks() try: task_num int(input(请输入要标记为完成的任务编号)) self.complete_task(task_num - 1) except ValueError: print(请输入有效的数字) elif choice 4: self.view_tasks() try: task_num int(input(请输入要删除的任务编号)) self.delete_task(task_num - 1) except ValueError: print(请输入有效的数字) elif choice 5: print(程序已退出) break else: print(无效的选择请重新输入) # 运行待办事项清单 if __name__ __main__: todo TodoList() todo.run()项目四批量文件重命名工具项目描述开发一个批量文件重命名工具支持对指定目录下的文件进行批量重命名操作。功能要求● 选择目标文件夹● 支持多种重命名模式● 预览重命名结果● 执行重命名操作● 错误处理和日志记录代码实现import os import shutil from datetime import datetime class BatchRenameTool: 批量文件重命名工具 def __init__(self): self.folder_path self.files [] self.rename_actions [] def select_folder(self): 选择目标文件夹 folder input(请输入要处理的文件夹路径).strip() if os.path.exists(folder) and os.path.isdir(folder): self.folder_path folder self.files [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))] print(f找到 {len(self.files)} 个文件) return True else: print(无效的文件夹路径) return False def show_files(self): 显示当前文件列表 if not self.files: print(暂无文件) return print(\n--- 当前文件列表 ---) for i, filename in enumerate(self.files): print(f{i1}. {filename}) def add_rename_action(self): 添加重命名操作 print(\n--- 重命名模式 ---) print(1. 添加前缀) print(2. 添加后缀) print(3. 替换文本) print(4. 按序号重命名) print(5. 转换为小写) print(6. 转换为大写) choice input(请选择重命名模式1-6) if choice 1: prefix input(请输入前缀) self.rename_actions.append((prefix, prefix)) print(f已添加前缀{prefix}) elif choice 2: suffix input(请输入后缀) self.rename_actions.append((suffix, suffix)) print(f已添加后缀{suffix}) elif choice 3: old_text input(请输入要替换的文本) new_text input(请输入替换后的文本) self.rename_actions.append((replace, old_text, new_text)) print(f已添加替换{old_text} → {new_text}) elif choice 4: start_num int(input(起始序号)) padding int(input(序号位数如001则输入3)) self.rename_actions.append((numbering, start_num, padding)) print(f已添加序号重命名从{start_num}开始{padding}位) elif choice 5: self.rename_actions.append((lower,)) print(已添加转换为小写操作) elif choice 6: self.rename_actions.append((upper,)) print(已添加转换为大写操作) else: print(无效的选择) def preview_rename(self): 预览重命名结果 if not self.rename_actions: print(请先添加重命名操作) return print(\n--- 重命名预览 ---) for i, old_name in enumerate(self.files): new_name old_name for action in self.rename_actions: action_type action[0] if action_type prefix: new_name action[1] new_name elif action_type suffix: name_part, ext_part os.path.splitext(new_name) new_name name_part action[1] ext_part elif action_type replace: new_name new_name.replace(action[1], action[2]) elif action_type numbering: start_num, padding action[1], action[2] name_part, ext_part os.path.splitext(new_name) new_name f{start_num i:0{padding}d}{ext_part} elif action_type lower: new_name new_name.lower() elif action_type upper: new_name new_name.upper() print(f{old_name} → {new_name}) def execute_rename(self): 执行重命名操作 if not self.rename_actions: print(请先添加重命名操作) return confirm input(\n确认执行重命名(y/n): ) if confirm.lower() ! y: print(操作已取消) return success_count 0 error_count 0 for i, old_name in enumerate(self.files): old_path os.path.join(self.folder_path, old_name) new_name old_name # 应用所有重命名操作 for action in self.rename_actions: action_type action[0] if action_type prefix: new_name action[1] new_name elif action_type suffix: name_part, ext_part os.path.splitext(new_name) new_name name_part action[1] ext_part elif action_type replace: new_name new_name.replace(action[1], action[2]) elif action_type numbering: start_num, padding action[1], action[2] name_part, ext_part os.path.splitext(new_name) new_name f{start_num i:0{padding}d}{ext_part} elif action_type lower: new_name new_name.lower() elif action_type upper: new_name new_name.upper() new_path os.path.join(self.folder_path, new_name) # 检查目标文件是否已存在 if os.path.exists(new_path): print(f跳过{old_name} → {new_name}目标文件已存在) error_count 1 continue try: os.rename(old_path, new_path) print(f成功{old_name} → {new_name}) success_count 1 except Exception as e: print(f失败{old_name} → {new_name}{e}) error_count 1 print(f\n重命名完成成功{success_count}失败{error_count}) self.rename_actions [] # 清空操作列表 def run(self): 运行批量重命名工具 print( 批量文件重命名工具 ) while True: print(\n--- 主菜单 ---) print(1. 选择文件夹) print(2. 查看文件列表) print(3. 添加重命名操作) print(4. 预览重命名结果) print(5. 执行重命名) print(6. 退出程序) choice input(\n请选择操作1-6) if choice 1: self.select_folder() elif choice 2: self.show_files() elif choice 3: self.add_rename_action() elif choice 4: self.preview_rename() elif choice 5: self.execute_rename() elif choice 6: print(程序已退出) break else: print(无效的选择请重新输入) # 运行批量文件重命名工具 if __name__ __main__: tool BatchRenameTool() tool.run()