The assignment I'm working on requests the following:
"Display all tasks in a manner that is easy to read. Make sure that each task is displayed with a corresponding number which can be used to identify the task."
I'm trying to add a variable called 'tasknum' that automatically loops through the preceding lines of the text file and increments the most recent value by one. In other words, counts how many lines (ie tasks) are already present, adds 1 and writes that new value to the tasknum variable in the task being added.
I'm unable to get the variable to increment. It stays on its initial value of 0 or seems to be skipping the for loop completely.
How can I make sure each new added line contains the variable tasknum with a value of +1 of the previous line?
tasks.txt contents:
admin, 1, Register Users, Add the usernames and passwords for all team members, 10 Oct 2019, 20 Oct 2019, No
admin, 2, Assign initial tasks, Assign each team member with appropriate tasks, 10 Oct 2019, 25 Oct 2019, No
My code thus far:
def add_task():
tasklist = open("tasks.txt", "a+")
# Requests user input to add tasks to list
user = input("Please enter your username\n")
tasknum = 0
tasktitle = input("Please enter the task title\n")
description = input("Please enter the task description\n")
dateadded = input("Please enter today's date\n")
datedue = input("Please enter the date this task is due\n")
status = "No"
# Task counter: increments task number
for tasknum in tasklist:
tasklist[1] = tasknum
tasknum += 1
# Writes user input to file and displays thank you message
tasklist.write(f"\n{user}, {tasknum}, {tasktitle}, {description}, {dateadded}, {datedue}, {status}")
print("Thanks, your task has been added")
tasklist.close()
if user == "admin" and login == True:
admin_menu()
else:
user_menu()