大家好!
前面我们从 Python 基础讲到自动化、可视化、爬虫,今天终于到了最有成就感的环节 ——实战小项目。
这些项目不是「纸上谈兵」的练习,而是能直接用、能解决实际问题、能体现学习成果的实用工具。
每个项目都附完整代码,新手跟着做,1 小时就能搞定一个!
一、为什么要做实战小项目?
✅ 巩固所学知识(比单纯看教程管用 10 倍)
✅ 做出看得见的成果(成就感拉满)
✅ 解决实际问题(办公 / 生活都能用)
✅ 简历加分(比「精通 Python 语法」更有说服力)
二、项目 1:天气查询小工具(实用指数⭐⭐⭐⭐⭐)
功能:输入城市名,一键查询实时天气(温度、湿度、风向、天气状况)。
适用场景:日常出行、办公提醒、自动生成天气日报。
准备工作
先安装依赖:
完整代码
import requestsdef get_weather(city): # 接口说明:使用免费天气接口(可自行替换) url = f"http://wthrcdn.etouch.cn/weather_mini?city={city}" try: response = requests.get(url) response.encoding = "utf-8" data = response.json() if data["desc"] == "OK": weather_info = data["data"]["forecast"][0] print("="*30) print(f"🏙️ 城市:{city}") print(f"🌡️ 温度:{data['data']['wendu']}℃") print(f"☁️ 天气:{weather_info['type']}") print(f"💧 湿度:{data['data']['shidu']}") print(f"💨 风向:{weather_info['fengxiang']}{weather_info['fengli']}") print(f"🌡️ 温度范围:{weather_info['low']} ~ {weather_info['high']}") print("="*30) else: print("❌ 未查询到该城市天气,请检查城市名!") except Exception as e: print(f"❌ 查询失败:{e}")if __name__ == "__main__": city = input("请输入要查询的城市名:") get_weather(city)
使用方法
三、项目 2:简易记账本(实用指数⭐⭐⭐⭐)
功能:记录收支、查看明细、统计总收支,自动保存到本地文件。
适用场景:个人记账、日常开销管理、小团队简易记账。
完整代码
import osfrom datetime import datetime# 记账文件FILE_NAME = "记账本.txt"def add_record(): """添加记账记录""" type_ = input("请输入类型(收入/支出):") money = input("请输入金额:") remark = input("请输入备注(如:吃饭、工资):") # 获取当前时间 time_str = datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 写入文件 with open(FILE_NAME, "a", encoding="utf-8") as f: f.write(f"{time_str}|{type_}|{money}|{remark}\n") print("✅ 记账成功!")def show_records(): """查看所有记录""" if not os.path.exists(FILE_NAME): print("📄 暂无记账记录!") return print("\n" + "="*50) print("📊 记账明细") print("="*50) total_income = 0 # 总收入 total_outcome = 0 # 总支出 with