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)