在以下项目中需要帮助,其中有两个类Task和To-Do-List。
我遇到以下错误。尝试解决,但仍会出现
输入选项3
输入任务:你好
追溯(最近一次通话):
中的文件“ /Users/vrishabpatel/Desktop/dominos/pythonExamples/toDoList/menu.py”,第75行 Menu()。run()
文件“ /Users/vrishabpatel/Desktop/dominos/pythonExamples/toDoList/menu.py”,第43行,在运行中
动作()
在add_tasks中,文件“ /Users/vrishabpatel/Desktop/dominos/pythonExamples/toDoList/menu.py”,第60行,
self.toDoList.new_task(task_name,complete =“ N”)
文件“ /Users/vrishabpatel/Desktop/dominos/pythonExamples/toDoList/toDoList.py”,第37行,位于new_task
self.tasks.append(Task(task_name,complete))
AttributeError:'ToDoList'对象没有属性'tasks'
from datetime import datetime
"""To Do List Programe"""
"""Represent a Tasks in the To-DoList.
match against a string in searches and store each
tasks"""
last_id = 0
class Task:
def __init__(self, task_name, complete=""):
self.task_name = task_name
self.complete = "N"
self.date_created = datetime.today().strftime('%d-%m-%y')
global last_id
last_id += 1
self.id = last_id
def match_task(self, filter):
"""Determine if this note matches the filter
text. Return True if it matches, False otherwise.
Search is not case sensitive and matches any word in the tasks. """
return filter.lower() in self.task_name.lower()
class ToDoList:
"""Represent a collection of tasks that
can be searched, modified and complete and deleted """
def __int__(self):
self.tasks = []
def new_task(self, task_name, complete):
"""Create new task and add it to the list"""
self.tasks.append(Task(task_name, complete))
def _find_task(self, task_id):
"""locate the task with given id"""
for task_name in self.tasks:
if str(task_name.id) == str(task_name.id):
return task_name
return None
def modify_task(self, task_id, task_name):
task_name = self._find_task(task_id)
if task_name:
task_name.task_name = task_name
return True
return False
def delete_task(self, task_id, complete):
task = self._find_task(task_id)
if task:
task.complete = "Y"
return self.tasks.remove(task_id-1)
return False
def search(self, filter):
"""Find all task that match the given
fliter string """
return [task for task in self.tasks if task.match(filter)]
和菜单类如下...
"""Main File to Run the programe"""
import sys
from toDoList import ToDoList
class Menu:
"""Display a menu and respond to choices when
run """
def __init__(self):
self.toDoList = ToDoList()
self.choices = {
"1": self.show_tasks,
"2": self.search_tasks,
"3": self.add_tasks,
"4": self.delete_tasks,
"5": self.quit,
}
def display_menu(self):
print(
"""
To Do List menu
===============
1. Show all Tasks
2. Search Tasks
3. Add Tasks
4. Delete Tasks
5. Quit
"""
)
def run(self):
"""Display the menu and repond to the choices"""
while True:
self.display_menu()
choice = input("Enter an option")
action = self.choices.get(choice)
if action:
action()
else:
print("{0} is not a valid choice".format(choice))
def show_tasks(self, tasks=None):
if not tasks:
tasks = self.toDoList.tasks
for task in tasks:
print("{0}: {1}".format(task.id, task))
def search_tasks(self):
filter = input("Search tasks:")
tasks = self.toDoList.search(filter)
self.show_tasks(tasks)
def add_tasks(self):
task_name = input("Enter a task:")
self.toDoList.new_task(task_name, complete="N")
print("Your task has been added:")
def delete_tasks(self):
id = input("Enter a task id:")
task = input("Enter task name:")
if task:
self.toDoList.delete_task(id, task)
def quit(self):
print("Thank you for using To-Do-List today")
sys.exit(0)
if __name__ == "__main__":
Menu().run()
答案 0 :(得分:1)
您的班级定义中有错字
class ToDoList:
"""Represent a collection of tasks that
can be searched, modified and complete and deleted """
def __int__(self):
self.tasks = []
应该是
class ToDoList:
"""Represent a collection of tasks that
can be searched, modified and complete and deleted """
def __init__(self):
self.tasks = []
__init__
不会被调用(由于输入错误),并且tasks
属性也不会被创建。