如何从字典中打印出一个类对象的一个​​属性或一系列属性?

时间:2019-03-07 19:38:41

标签: python class dictionary printing attributes

我正在做一个待办事项列表管理器。列表中的每个项目都是一个属于“任务”类的对象,该对象具有所示的属性。每次用户调用函数“ taskmaker”时,都会将Task对象添加到字典taskDict中,其中的键(计数)对于创建的每个任务递增一个。

然后,程序使用print(taskDict)将创建的任务列表及其各自的键打印给我。

当前,我获得了对象的生成名称和密钥。我只想显示每个对象的属性,例如用户定义的“任务名称”和“期限”。

import datetime


class Task:
    def __init__(self, taskname, datecreated, iscomplete, datecomplete, duedate, duetime, importance, isproject, projectname, category, repeat, timetodo, scheduledate):
        self.taskname = taskname
        self.datecreated = datecreated
        self.iscomplete = iscomplete
        self.datecomplete = datecomplete
        self.duedate = duedate
        self.duetime = duetime
        self.importance = importance
        self.isproject = isproject
        self.projectname = projectname
        self.category = category
        self.repeat = repeat
        self.timetodo = timetodo
        self.scheduledate = scheduledate


taskDict = {}
count = 0       # This is the key

decide: str = input("If you would like to add a task, type anything except 0. : ")


def taskmaker():
    global decide
    while decide != "0":
        namer = input("Enter your task: ")
        datecreateder = datetime.datetime.now()
        duedater = input("When would you like this due? : ")
        duetimer = input("At what time would you like this due? : ")
        task = Task(namer, datecreateder, False, "", duedater, duetimer, 0, False, "", "", False, 1, "")
        global count
        taskDict[count] = task
        count = count + 1
        decide = input("If you would like to add a task, type anything except 0. : ")


taskmaker()

print(taskDict)

我没有运气找到没有被严重否决的资源,这些资源指向如何做到这一点。

这是控制台的输入和输出。

If you would like to add a task, type anything except 0. : 1
Enter your task: Chemistry homework
When would you like this due? : Tomorrow
At what time would you like this due? : Noon
If you would like to add a task, type anything except 0. : 2
Enter your task: History Essay
When would you like this due? : Wednesday
At what time would you like this due? : Midnight
If you would like to add a task, type anything except 0. : 0
{0: <__main__.Task object at 0x000001AFBADCCEB8>, 1: <__main__.Task object at 0x000001AFBADCCE48>}

0 个答案:

没有答案
相关问题