【问题标题】:Simple way of adding task numbers to text file将任务编号添加到文本文件的简单方法
【发布时间】:2020-02-08 02:14:58
【问题描述】:

谁能告诉我一种将任务编号添加到我的输出文本文件的简单方法。我所需要的只是每次添加新任务时的简单方法,每次创建新任务时,代码都应该循环并在描述中添加一个新数字。我还需要稍后在代码中通过输入任务编号来访问任务。

当前输出到文本文件:

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

【问题讨论】:

    标签: python loops external-data-source


    【解决方案1】:

    试试下面的代码:

    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: "+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)
    
    
    

    【讨论】:

    • 代码似乎可以运行,但只添加了数字 1 作为任务编号。当我保存下一个任务时,任务仍然显示为 1 而不是 2
    • 更新了答案,现在试试。之前我在 add_task() 中设置 count=0,它正在重置计数器。
    • 非常感谢您的帮助。得到另一个错误:count=count+1。分配前引用的“计数”
    • 非常感谢您的所有帮助,但仍将两个任务保存为任务 1。第二个任务未显示任务 2
    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    相关资源
    最近更新 更多