0

Could someone please show me a simple way of adding task numbers to my output text file. All I need is a simple way for every time a new task is added,the code should loop and add a new number to the description every time a new task is created. I need to also be able to access the task later in the code by entering the task number.

output to text file currently:

User assigned to task:
admin
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:
admin
Task Title:
jog
Task Description:
go jogging
Task Due Date:
2020-02-08
Date Assigned:
2020-02-07
Task Completed:
No

My current code:

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()
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

Try below code:

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: "+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")
count = 0
add_task(count)


Anupam Chaplot
  • 1,134
  • 1
  • 9
  • 22
  • the code seems to run but adds only number 1 as the task number. As I save the next task the task still displays as 1 and not 2 –  Feb 07 '20 at 17:55
  • Updated the answer, try now. Earlier I was setting count=0 inside the add_task(), which was resetting the counter. – Anupam Chaplot Feb 07 '20 at 18:02
  • Thank you so much for the help. Getting another error: count=count+1. 'count' referenced before assignment –  Feb 07 '20 at 18:12
  • Thank you so much for all the help but still saving both tasks as task 1. The second task is not displaying task 2 –  Feb 07 '20 at 18:26