我希望有人能最终提供帮助。我正在尝试编写代码以将任务保存到文本文件中。文本文件从用户那里获取输入并存储信息。我想找到一种非常简单的方法来更改我的以下代码以向任务添加一个数字,以便我稍后能够调用特定任务。在第一个任务标记为用户分配给任务 1 后:下一个任务应标记为用户分配给任务 2:然后任务用户分配给任务 3:
任务示例:
分配给任务的用户:
杰克
任务名称:
慢跑
任务描述:
去慢跑
任务截止日期:
2020-02-08
分配日期:
2020-02-07
任务完成:
不
要求的输出:
分配给任务 1 的用户:
杰克
任务名称:
慢跑
任务描述:
去慢跑
任务截止日期:
2020-02-08
分配日期:
2020-02-07
任务完成:
不
我到目前为止的代码如下。它正在将数字写入文本文件,但它们都标记为任务 1,并且下一个任务不会更改为任务 2:
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")
count = 0
add_task(count)