In a program task.manager.py that I' m writing, I do need to edit some data in a .txt file but my program fail. I've tried alternative solution but can't identify the issue. Can you please help me?
Here the piece of code failing:
def view_mine():
tasks = []
with open("tasks.txt", 'r+') as tasks_file:
for line in tasks_file:
user, title, description, due_date, assigned_date, is_completed = line.split (';')
if username == user:
tasks.append((user, title, description,due_date, assigned_date, is_completed.strip()))
if tasks:
for index, task in enumerate(tasks, start=1):
user, title, description,due_date, assigned_date, is_completed = task
print (f"""
Task number: {index}
User: {user}
Task name: {title}
Task description: {description}
Due date: {due_date}
Assigned date: {assigned_date}
Task completed: {is_completed}
""")
choice = input("Please enter the task number you want to work on (-1 to go back to the main menu): ")
if choice.isdigit():
choice = int(choice)
if 1 <= choice <= len(tasks):
task = tasks[choice -1]
mark_complete = input ("Do you want to mark this task as complete? (Yes/No): ")
if mark_complete.lower() == "yes":
task = list(task)
task[5] = "Yes"
tasks[choice -1] = tuple(task)
print ("Task marked as complete!")
else:
edit_task = input("Do you want to edit this task? (Yes/No): ")
if edit_task.lower() == "yes":
new_user = input("Please enter the new username for the task: ")
new_due_date = input("Please enter the new due date for the task: ")
new_assigned_date = input("Please enter the new assigned date for the task: ")
task = list(task)
task[0] = new_user
task[3] = new_due_date
task[4] = new_assigned_date
task[choice - 1] = tuple(task)
print("Task updated!")
When editing a data, I expect the .txt file updated with the new data. But doesn't work.