0

有人可以帮助我当前的代码。我想将任务编号添加到保存在输出文本文档中的任务中。我需要循环它,以便为每个任务分配下一个任务编号。如果可能的话,我希望以后能够调用这些任务编号。

到目前为止,我的代码是:

def add_task():
 if menu == "a" or menu == "A":
    with open( 'user.txt' ) as fin :    
        usernames = [i.split(',')[0] for i in fin.readlines() if len(i) > 3]
        task = input ("Please enter the username of the person the task is assigned to.\n")
    while task not in usernames :
        task = input("Username not registered. Please enter a valid username.\n")

    else:
        task_title = input("Please enter the title of the task.\n")
        task_description = input("Please enter the task description.\n")
        task_due = input("Please input the due date of the task. (yyyy-mm-dd)\n")
        date = datetime.date.today()
        task_completed = False
        if task_completed == False:
            task_completed = "No"
        else:
            task_completed = ("Yes")

        with open('tasks.txt', 'a') as task1:
            task1.write("\nUser assigned to task:\n" + task + "\nTask Title :"  + "\n" + task_title + "\n" + "Task Description:\n" + task_description + "\n" + "Task Due Date:\n" + task_due + "\n" + "Date Assigned:\n" + str(date) + "\n" + "Task Completed:\n" + task_completed + "\n")
            print("The new assigned task has been saved")
add_task()
4

1 回答 1

0

首先,我真的不想详细介绍,但是您存储输出的方式效率非常低,并且难以访问较大的文本文件。为什么不使用一些免费的数据库系统来存储您的数据。

第二。假设您想一次编写许多任务,但可以说只“保存”一次,请考虑使用 dicts 的 dict。

def write_task_to_txt(task):
    ### break down the dict to from your lines to write to your text
def add_task(task_list,task_id):

    new_tasks[task_id] = {}
    new_tasks[task_id]["username"] = "username"
    new_tasks[task_id]["title"] = "Task 1"
    ### How you fill up a task
    return task_list
new_tasks = {}
for i in range(10):
    new_tasks = add_task(new_tasks,i+1)
write_task_to_txt(new_tasks)

有了这个,您总是可以使用 new_tasks[task_id] 访问任务以提取该任务的所有数据。注意 for 循环使用的是迭代器。如果您想避免这种情况,您可以使用全局和 while 循环。但是,如果您想这样做,我建议将您的应用程序转换为类并改用类变量。

这是我将如何尝试的框架:

class yourclass():
    def __init__(self):
        self.task_num = 1 #use 1 if no values
        self.tasks_towrite = {}
        self.mode_select()
    def mode_select(self):
        self.menu = input("choose mode")
        while(1):
            if self.menu == "a" or self.menu == "A":
                self.add_task()
            if self.menu == "s".casefold() #Cool function that does the same as your menu thingy
                self.write_to_text()
            else:
                print("exit")
                self.close_program()
    def close_program(self): # Exit function
        print("exiting")
        sys.exit(1)

    def add_task(self): #Add task

        with open( 'user.txt' ) as fin :    
            self.usernames = [i.split(',')[0] for i in fin.readlines() if len(i) > 3]
            task = input ("Please enter the username of the person the task is assigned to.\n")
        while task not in self.usernames :
            task = input("Username not registered. Please enter a valid username.\n")

        else:
            new_task = {}
            new_task["username"] = task
            new_task["title"] = input("Please enter the title of the task.\n")
            new_task["description"] = input("Please enter the task description.\n")
            new_task["due"] = input("Please input the due date of the task. (yyyy-mm-dd)\n")

            date = datetime.date.today()
            task_completed = False
            if task_completed == False:
                 new_task["completed"]  = "No"
            else:
                 new_task["completed"]  = "Yes"
            new_task["assigned"] = date
            self.tasks_towrite[self.task_num] = new_task
            sefl.task_num +=1 #New test number
            return None #returns to mode_select
    def write_to_text(self):
        with open('tasks.txt', 'a') as task1:
            for i in self.tasks_towrite:
                task1.write(str(i) + "\n") #Writes it all at once You can reformat however you want 
            self.tasks_towrite = {}

            print("The new assigned tasks has been saved")
            return None #returns to menu or you could go to self.close_program to exit 

if __name__== '__main__':
x = yourclass()
于 2020-02-07T16:07:35.843 回答