Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

python-3.7 - 待办事项项目(To-Do-List project)

need help in following project where two classes Task and To-Do-List.

(在以下项目中需要帮助,其中有两个类Task和To-Do-List。)

I am getting following errors.

(我收到以下错误。)

Trying to resolve but still coming up

(试图解决但仍在继续)

Enter an option3

(输入一个选项3)
Enter a task:hello

(输入任务:你好)
Traceback (most recent call last):

(追溯(最近一次通话):)
File "/Users/vrishabpatel/Desktop/dominos/pythonExamples/toDoList/menu.py", line 75, in

(文件“ /Users/vrishabpatel/Desktop/dominos/pythonExamples/toDoList/menu.py”,第75行,在)
Menu().run()

(Menu()。run())
File "/Users/vrishabpatel/Desktop/dominos/pythonExamples/toDoList/menu.py", line 43, in run

(运行中的文件“ /Users/vrishabpatel/Desktop/dominos/pythonExamples/toDoList/menu.py”,第43行)
action()

(行动())
File "/Users/vrishabpatel/Desktop/dominos/pythonExamples/toDoList/menu.py", line 60, in add_tasks

(add_tasks中的文件“ /Users/vrishabpatel/Desktop/dominos/pythonExamples/toDoList/menu.py”,第60行)
self.toDoList.new_task(task_name, complete="N")

(self.toDoList.new_task(task_name,complete =“ N”))
File "/Users/vrishabpatel/Desktop/dominos/pythonExamples/toDoList/toDoList.py", line 37, in new_task

(文件“ /Users/vrishabpatel/Desktop/dominos/pythonExamples/toDoList/toDoList.py”,行37,在new_task中)
self.tasks.append(Task(task_name, complete))

(self.tasks.append(任务(task_name,完成)))
AttributeError: 'ToDoList' object has no attribute 'tasks'

(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)]

And Menu class as follows...

(和Menu类如下...)

"""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()

  ask by Jay Patel translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a typo in your class definition

(您的班级定义中有错字)

class ToDoList:
    """Represent a collection of tasks that 
    can be searched, modified and complete and deleted """

    def __int__(self):
        self.tasks = []

should be

(应该)

class ToDoList:
    """Represent a collection of tasks that 
    can be searched, modified and complete and deleted """

    def __init__(self):
        self.tasks = []

The __init__ doesn't get called (because of a typo) and the tasks attribute never gets created.

(__init__不会被调用(由于输入错误),并且tasks属性永远不会被创建。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...