-1

Could anyone help with the correct way to loop to a text file. I have an issue with my code at the moment where it is writing task numbers to a text file but all the numbers are returning task 1. My second task is not returning task 2 and the third task not returning task 3. They are all returning task 1. I would like for the task number to change every time i add a task to the text file. Would a simple way to be able to call on a specific task later in the code be possible?

task example:

User assigned to task:

jack

Task Title:

jog

Task Description:

Go jogging

Task Due Date:

2020-02-08

Date Assigned:

2020-02-07

Task Completed:

No

requested output:

User assigned to task 1:

jack

Task Title:

jog

Task Description:

Go jogging

Task Due Date:

2020-02-08

Date Assigned:

2020-02-07

Task Completed:

No

The code i have so far is as follows. It is writing numbers to the text file but they are all labelled task 1 and the next task is not changing to task 2:

count = 0

def add_task(count):
 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:
            count = count + 1
            task1.write("\nUser assigned to task" + str(count) + "\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(count)

1 Answers1

2

The variable count is in the global scope, and when you run count = count + 1 in your function, you change it only for the local function scope. if you want the function to change a global variable, you need to use the global statement.

Add this as the first line of add_task function:

global count

And remove count from the function parameter. (so add_task function will be called without passing the count to it)

Now when you run count = count + 1, it will change the count variable from the global scope.

However, using global variables is not recommended, a better approach would be to use a class, and save count as class variable:

class TaskCreator():
    task_count = 0
    @classmethod
    def add_task(cls):
        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:
                    cls.task_count += 1
                    task1.write("\nUser assigned to task" + str(cls.task_count) + "\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")

TaskCreator.add_task()
Moshe perez
  • 1,626
  • 1
  • 8
  • 17