0

Could someone help with my current code. I would like to add task numbers to my tasks that get saved in my output text document. I would need to loop it so each task will be assigned the next task number. If possible I would like to be able to call on these task numbers later.

My code so far is:

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

1 Answers1

0

Firstly, I don't really want to go into detail but the way you are storing your output is highly inefficient and difficult to access the larger your text file gets. Why not use some free DB system to store your data.

Secondly. Assuming that you want to write many tasks at a time but only "save" once so to speak, consider using a dict of dicts.

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)

With this you can always access the task using new_tasks[task_id] to pull all the data of that task. Note the for loop is using an iterator. If you want to avoid this you could use a global and a while loop instead. BUT if you want to do that, i recommend converting your application into a class and use class variables instead.

Here is a skeleton of how I would try that:

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()
Jason Chia
  • 1,144
  • 1
  • 5
  • 18
  • I'm not sure why but after entering the due date its looping back and asking for user name input again?? any ideas? Thank you for the help btw –  Feb 07 '20 at 16:20
  • what do you mean? For which code sample? 1st or second? Either way, both my code samples require the full process as running add_task again which is what you asked for right? Otherwise you can simply add another input-> if-else to add a task to the same user name in a while loop as use break to get out of it. – Jason Chia Feb 10 '20 at 10:00