当前位置: 首页 > news >正文

Python3 【函数】项目实战:5 个新颖的学习案例

Python3 【函数】项目实战:5 个新颖的学习案例

本文包含5编程学习案例,具体项目如下:

  1. 简易聊天机器人
  2. 待办事项提醒器
  3. 密码生成器
  4. 简易文本分析工具
  5. 简易文件加密解密工具

项目 1:简易聊天机器人

功能描述:
实现一个简易聊天机器人,根据用户输入返回预设的响应。

代码:

def chatbot_response(user_input):responses = {"hello": "Hello! How can I help you?","how are you": "I'm just a bot, but I'm doing great!","bye": "Goodbye! Have a nice day!","default": "I'm not sure how to respond to that."}return responses.get(user_input.lower(), responses["default"])# 测试案例
print(chatbot_response("Hello"))  # 输出: Hello! How can I help you?
print(chatbot_response("How are you"))  # 输出: I'm just a bot, but I'm doing great!
print(chatbot_response("What's your name?"))  # 输出: I'm not sure how to respond to that.

执行结果:

Hello! How can I help you?
I'm just a bot, but I'm doing great!
I'm not sure how to respond to that.

项目 2:简易待办事项提醒器

功能描述:
实现一个简易待办事项提醒器,支持添加任务、设置提醒时间,并在指定时间提醒用户。

代码:

import time
from datetime import datetimedef add_task(tasks, task, reminder_time):tasks.append({"task": task, "reminder_time": reminder_time})def check_reminders(tasks):current_time = datetime.now()for task in tasks:if current_time >= task["reminder_time"]:print(f"Reminder: {task['task']} is due now!")tasks.remove(task)# 测试案例
tasks = []
add_task(tasks, "Buy groceries", datetime(2025, 1, 27, 10, 26))  # 设置提醒时间为 2025-1-27 10:26
add_task(tasks, "Read a book", datetime(2025, 1, 27, 10, 28))    # 设置提醒时间为 2025-1-27 10:28while tasks:check_reminders(tasks)time.sleep(60)  # 每分钟检查一次

执行结果:

Reminder: Buy groceries is due now!  # 当时间到达 2025-1-27 10:26 时输出
Reminder: Read a book is due now!    # 当时间到达 2025-1-27 10:28 时输出

项目 3:密码生成器

功能描述:
实现一个密码生成器,生成包含大小写字母、数字和特殊字符的随机密码。

代码:

import random
import stringdef generate_password(length=12):characters = string.ascii_letters + string.digits + string.punctuationpassword = ''.join(random.choice(characters) for _ in range(length))return password# 测试案例
print("Generated Password:", generate_password())  # 输出: 随机生成的密码,如 "A1b@C3d$E5f^"

执行结果:

Generated Password: A1b@C3d$E5f^

项目 4:简易文本分析工具

功能描述:
实现一个简易文本分析工具,统计文本中的单词数量、字符数量和最常见的单词。

代码:

from collections import Counterdef analyze_text(text):words = text.split()word_count = len(words)char_count = len(text)most_common_word = Counter(words).most_common(1)[0][0]return {"word_count": word_count,"char_count": char_count,"most_common_word": most_common_word}# 测试案例
text = "This is a simple text analysis tool. It counts words and characters."
result = analyze_text(text)
print(result)

执行结果:

{'word_count': 12,'char_count': 68,'most_common_word': 'This'
}

项目 5:简易文件加密解密工具

功能描述:
实现一个简易文件加密解密工具,使用简单的字符替换算法对文件内容进行加密和解密。

代码:

def encrypt(text, key):encrypted_text = ""for char in text:encrypted_text += chr(ord(char) + key)return encrypted_textdef decrypt(encrypted_text, key):decrypted_text = ""for char in encrypted_text:decrypted_text += chr(ord(char) - key)return decrypted_textdef encrypt_file(input_file, output_file, key):with open(input_file, 'r') as file:text = file.read()encrypted_text = encrypt(text, key)with open(output_file, 'w') as file:file.write(encrypted_text)def decrypt_file(input_file, output_file, key):with open(input_file, 'r') as file:encrypted_text = file.read()decrypted_text = decrypt(encrypted_text, key)with open(output_file, 'w') as file:file.write(decrypted_text)# 测试案例
encrypt_file("input.txt", "encrypted.txt", 3)  # 加密文件
decrypt_file("encrypted.txt", "decrypted.txt", 3)  # 解密文件

执行结果:

  • 生成 encrypted.txt,内容为加密后的文本。
  • 生成 decrypted.txt,内容与原始文件 input.txt 相同。

总结

以上 5 个迷你项目涵盖了密码生成、待办事项提醒、聊天机器人、文本分析和文件加密解密等新颖且实用的功能。每个项目都附有测试案例和执行结果,适合用于学习和练习 Python 函数的综合应用。

http://www.lryc.cn/news/527755.html

相关文章:

  • XSS 漏洞全面解析:原理、危害与防范
  • 从 GShard 到 DeepSeek-V3:回顾 MoE 大模型负载均衡策略演进
  • 【回溯+剪枝】回溯算法的概念 全排列问题
  • Flutter解决macbook M芯片Android Studio中不显示IOS真机的问题
  • 自签证书的dockerfile中from命令无法拉取镜像而docker的pull命令能拉取镜像
  • 【MySQL】--- 复合查询 内外连接
  • QT TLS initialization failed
  • 系统学英语 — 句法 — 复合句
  • 指针的介绍2前
  • 16.Word:石油化工设备技术❗【28】
  • Python-基础环境(01) 虚拟环境,Python 基础环境之虚拟环境,一篇文章助你完全搞懂!
  • Dest1ny漏洞库:用友 U8-CRM 系统 ajaxgetborrowdata.php 存在 SQL 注入漏洞
  • java.sql.Date 弃用分析与替代方案
  • HarmonyOS:状态管理最佳实践
  • 如何提高新产品研发效率
  • MongoDB平替数据库对比
  • JavaScript系列(46)-- WebGL图形编程详解
  • YOLO目标检测4
  • 十三先天记
  • 【论文阅读笔记】“万字”关于深度学习的图像和视频阴影检测、去除和生成的综述笔记 | 2024.9.3
  • Android AOP:aspectjx
  • 前端【11】HTML+CSS+jQUery实战项目--实现一个简单的todolist
  • 2025课题推荐——USBL与DVL数据融合的实时定位系统
  • 滑动窗口详解:解决无重复字符的最长子串问题
  • 第05章 11 动量剖面可视化代码一则
  • MySQL的复制
  • Cpp::IO流(37)
  • 基于OpenCV实现的答题卡自动判卷系统
  • 如何将电脑桌面默认的C盘设置到D盘?详细操作步骤!
  • 二十三种设计模式-享元模式