大家好!欢迎来到奥饼饼智慧谷 📚。今天我们来做一个对学习很有帮助的小工具——背单词小程序!用游戏的方式背单词,让学习更有趣!
🎯 功能介绍
这个背单词小程序会有这些功能:
💡 学习小贴士
每天背10个,坚持就是胜利!
📚 开始写代码
以下是完整的背单词小程序代码:
import json import os import random DATA_FILE = "vocabulary.json" def load_data(): """从文件加载单词数据""" if os.path.exists(DATA_FILE): with open(DATA_FILE, 'r', encoding='utf-8') as f: return json.load(f) return [] def save_data(data): """保存单词数据到文件""" with open(DATA_FILE, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=2) def add_word(data): """添加新单词""" print("\n" + "="*40) print(" ➕ 添加新单词") print("="*40) while True: word = input("\n请输入单词: ").strip().lower() if word: break print("❌ 单词不能为空!") while True: meaning = input("请输入中文意思: ").strip() if meaning: break print("❌ 意思不能为空!") # 检查是否已存在 for item in data: if item["word"] == word: print("\n⚠️ 这个单词已经在单词本里了!") return # 添加新单词 new_word = { "word": word, "meaning": meaning, "correct": 0, "wrong": 0, "mastered": False } data.append(new_word) save_data(data) print(f"\n✅ 已添加: {word} - {meaning}") def view_words(data): """查看所有单词""" if not data: print("\n📭 单词本是空的,先去添加单词吧!") return print("\n" + "="*60) print(f" 📖 单词本 (共 {len(data)} 个单词)") print("="*60) print(f"{'单词':<20} {'意思':<20} {'掌握':<10} 统计") print("-"*60) for item in data: mastered = "✅ 已掌握" if item["mastered"] else "⏳ 学习中" stats = f"对:{item['correct']} 错:{item['wrong']}" print(f"{item['word']:<20} {item['meaning']:<20} {mastered:<10} {stats}") print("="*60) def quiz(data): """单词测试""" if not data: print("\n📭 单词本是空的,先去添加单词吧!") return print("\n" + "="*40) print(" 🎯 单词测试") print("="*40) # 选择要测试的单词 print("\n1. 测试所有单词") print("2. 只测试未掌握的") print("3. 只测试容易错的") choice = input("\n请选择 (1-3): ").strip() if choice == "2": test_words = [w for w in data if not w["mastered"]] elif choice == "3": test_words = [w for w in data if w["wrong"] > w["correct"]] else: test_words = data if not test_words: print("\n📭 没有符合条件的单词!") return # 开始测试 random.shuffle(test_words) score = 0 total = len(test_words) print(f"\n📝 共 {total} 个单词,开始吧!") print("输入 q 可以随时退出\n") for i, item in enumerate(test_words, 1): print(f"\n第 {i}/{total} 题") print(f"💬 {item['word']}") answer = input("中文意思是? ").strip().lower() if answer == 'q': print("\n🚪 退出测试...") break if answer == item["meaning"]: print("🎉 答对了!") item["correct"] += 1 score += 1 # 连续答对5次视为掌握 if item["correct"] >= 5 and item["correct"] > item["wrong"]: item["mastered"] = True print("⭐ 恭喜,这个单词你已经掌握了!") else: print(f"❌ 答错了! 正确答案是: {item['meaning']}") item["wrong"] += 1 save_data(data) # 显示结果 print("\n" + "="*40) print(f" 📊 测试结果: {score}/{total}") if total > 0: percentage = (score / total) * 100 print(f" 正确率: {percentage:.1f}%") if percentage >= 90: print(" 🏆 太棒了!") elif percentage >= 70: print(" 👍 不错哦!") elif percentage >= 50: print(" 💪 继续加油!") else: print(" 📖 多复习一下!") print("="*40) def add_sample_words(data): """添加示例单词""" samples = [ {"word": "python", "meaning": "蟒蛇", "correct": 0, "wrong": 0, "mastered": False}, {"word": "program", "meaning": "程序", "correct": 0, "wrong": 0, "mastered": False}, {"word": "code", "meaning": "代码", "correct": 0, "wrong": 0, "mastered": False}, {"word": "learn", "meaning": "学习", "correct": 0, "wrong": 0, "mastered": False}, {"word": "happy", "meaning": "开心", "correct": 0, "wrong": 0, "mastered": False} ] for sample in samples: exists = False for item in data: if item["word"] == sample["word"]: exists = True break if not exists: data.append(sample) save_data(data) print("✅ 已添加示例单词!") def main(): print("="*40) print(" 📚 欢迎使用背单词小程序! 📚") print("="*40) data = load_data() if not data: print("\n📝 单词本是空的,先添加几个示例单词?") choice = input("添加示例单词吗? (y/n): ").strip().lower() if choice == 'y': add_sample_words(data) while True: print("\n" + "-"*40) print("1. 单词测试") print("2. 添加新单词") print("3. 查看单词本") print("4. 退出") print("-"*40) choice = input("\n请选择 (1-4): ").strip() if choice == "1": quiz(data) elif choice == "2": add_word(data) elif choice == "3": view_words(data) elif choice == "4": print("\n👋 再见! 每天进步一点点!") break else: print("❌ 请选择 1-4!") if __name__ == "__main__": main()
学习是一场马拉松,不是短跑!每天进步一点点,终会到达终点!
🚀 运行试试吧!
保存为 `vocab.py` 然后运行:
python vocab.py
开始背单词之旅吧!添加你需要的单词,每天测试一下!
✨ 进阶玩法
想让这个背单词程序更强大?试试这些改进:
🎁 学习建议
每天固定时间背单词,效果更好!
📚 知识要点
通过这个项目,我们学到了:
今天的背单词小程序就到这里了!快去把需要背的单词加进去,每天测试一下,见证自己的进步!📚
👋 感谢阅读 | 奥饼饼智慧谷
每天一个有趣的编程小项目,让学习变成乐趣!
💡 点赞 + 在看 + 分享,让更多人一起享受编程的快乐!