如何为Python待办事项聊天机器人实现任务完成后的删除线标记功能
如何为Python待办事项聊天机器人实现任务完成后的删除线标记功能
看起来你的Toodle-Doo机器人已经有了基础的待办列表框架,现在要实现“完成任务后添加删除线”的功能对吧?我来一步步帮你调整代码,完全贴合你现有的结构~
核心问题拆解
首先得解决两个关键问题:
- 原来的待办列表是
handle_ChoresList()里的临时变量,函数执行完就消失了,得把列表状态持久保存下来 - 要能识别用户“Done with Laundry”这类输入,找到对应任务并标记完成,打印时用
~~语法实现删除线效果
第一步:重写ChoresList.py(保存状态+新增完成功能)
我们把原来的临时列表改成模块级的全局变量,用字典记录每个任务的「名称」和「完成状态」,再新增两个函数:一个负责打印带删除线的列表,一个负责处理标记任务完成的逻辑。
# ChoresList.py # 模块级全局变量:保存待办列表,每个元素是字典{"task": 任务名, "completed": 是否完成} chores_list = [] # 打印待办列表(支持删除线格式) def print_chores_list(): print("\nYour Activities:") for idx, item in enumerate(chores_list, 1): if item["completed"]: # 已完成任务用~~包裹实现Markdown删除线效果 print(f"~~{idx}. {item['task']}~~") else: print(f"{idx}. {item['task']}") # 初始化/添加待办任务 def handle_ChoresList(): global chores_list # 清空旧列表(如果之前创建过的话) chores_list = [] print("\nOkay! Please type in your list of activities you want to get done today." " When you're finished, type 'complete'!") count = 1 while True: item = input(f"{count}. ").strip() if item.lower() == "complete": # 打印刚创建好的待办列表 print_chores_list() break if item == "": print("\nPlease enter an activity, or type 'complete' if you are done.") continue # 把任务添加到全局列表,默认状态为未完成 chores_list.append({"task": item, "completed": False}) count += 1 # 标记任务完成:处理用户输入的"Done with Laundry"这类内容 def mark_task_completed(user_input): global chores_list if not chores_list: # 如果还没创建过待办列表,提示用户先创建 print("\nYou haven't created a to-do list yet! Let's make one first :)") return False # 统一转小写,避免大小写匹配问题 user_input_lower = user_input.lower() # 匹配常见的完成触发短语 trigger_phrases = ["done with ", "finished ", "completed "] task_name = None for phrase in trigger_phrases: if phrase in user_input_lower: task_name = user_input_lower.split(phrase)[-1].strip() break # 如果没匹配到触发短语,直接拿整个输入去匹配任务 if not task_name: task_name = user_input_lower # 遍历列表找匹配的任务 matched = False for item in chores_list: if task_name in item["task"].lower(): item["completed"] = True matched = True break if matched: # 打印更新后的列表 print_chores_list() # 随机输出一句鼓励的话 import random encouragements = ["Nice job!", "Keep it up!", "Great work!", "You're crushing it!"] print(f"\n{random.choice(encouragements)}") return True else: print("\nHmm, I can't find that task in your list. Did you spell it right?") return False
第二步:修改main.py(新增完成任务的判断逻辑)
先修正你原来代码里的小bug(print("\nAnything else...")后面少了闭合括号),然后在主循环里新增判断:如果用户输入是要完成任务,就调用上面的mark_task_completed()函数。
# Toodle-Doo Main.py from ChoresList import handle_ChoresList, mark_task_completed # ------------------------------------------------- quitKeyWords = ['quit', 'exit', 'done', 'finished', 'all good'] listKeyWords = ['to-do', 'to do', 'list', 'chores'] # 新增完成任务的触发短语 completeTaskKeyWords = ['done with', 'finished', 'completed'] # ------------------------------------------------- def main(): print("Hello, I'm Toodle-Doo! A chatbot tool made to help you stay productive.") print("\nWhat would you like to do today?\n") while True: userInput = input().strip() # 退出逻辑 if any(word in userInput.lower() for word in quitKeyWords): print("\nGood work today! Take care :)") break # 标记任务完成逻辑 elif any(phrase in userInput.lower() for phrase in completeTaskKeyWords): mark_task_completed(userInput) print("\nAnything else you'd like me to help out with?\n") # 待办列表逻辑 elif any(word in userInput.lower() for word in listKeyWords): handle_ChoresList() print("\nAnything else you'd like me to help out with?\n") else: print("\nI'm not sure what to say to that. Please enter something else.\n") if __name__ == "__main__": main()
测试效果示例
现在运行机器人,交互流程会是这样:
Hello, I'm Toodle-Doo! A chatbot tool made to help you stay productive. What would you like to do today? to do list Okay! Please type in your list of activities you want to get done today. When you're finished, type 'complete'! 1. Laundry 2. Grocery shopping 3. complete Your Activities: 1. Laundry 2. Grocery shopping Anything else you'd like me to help out with? Done with Laundry Your Activities: ~~1. Laundry~~ 2. Grocery shopping Nice job! Anything else you'd like me to help out with? quit Good work today! Take care :)
可选小优化
- 支持用序号完成任务:比如输入“Done 1”就标记第一个任务完成,操作更快捷
- 允许向现有列表追加任务:不用每次创建新列表就清空旧的
- 把鼓励语、关键词都抽成配置变量,后续修改更方便
这样改完,你的机器人就完全实现你想要的功能啦😉




