0

出力テキスト ファイルにタスク番号を追加する簡単な方法を教えてください。私が必要とするのは、新しいタスクが追加されるたびにコードがループして、新しいタスクが作成されるたびに新しい番号を説明に追加する簡単な方法だけです。タスク番号を入力して、コードの後半でタスクにアクセスできるようにする必要もあります。

現在のテキストファイルへの出力:

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

要求された出力:

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

私の現在のコード:

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()
4

1 に答える 1