首页 > 解决方案 > 循环到文本文件的正确函数

问题描述

任何人都可以帮助以正确的方式循环到文本文件。我的代码在将任务编号写入文本文件时遇到问题,但所有数字都返回任务 1。我的第二个任务没有返回任务 2,第三个任务没有返回任务 3。它们都在返回任务 1. 每次将任务添加到文本文件时,我都希望更改任务编号。在代码中稍后能够调用特定任务的简单方法是否可行?

任务示例:

分配给任务的用户:

杰克

任务名称:

慢跑

任务描述:

去慢跑

任务截止日期:

2020-02-08

分配日期:

2020-02-07

任务完成:

要求的输出:

分配给任务 1 的用户:

杰克

任务名称:

慢跑

任务描述:

去慢跑

任务截止日期:

2020-02-08

分配日期:

2020-02-07

任务完成:

我到目前为止的代码如下。它正在将数字写入文本文件,但它们都标记为任务 1,并且下一个任务不会更改为任务 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)

标签: pythonfunctionloops

解决方案


该变量count在全局范围内,当您count = count + 1在函数中运行时,您仅针对本地函数范围更改它。如果你想让函数改变一个全局变量,你需要使用global语句。

将此添加为add_task函数的第一行:

global count

并从函数参数中删除计数。(因此add_task将调用函数而不将计数传递给它)

现在,当您运行时count = count + 1,它将更改全局范围内的 count 变量。

但是,不建议使用全局变量,更好的方法是使用一个类,并将计数保存为类变量:

class TaskCreator():
    task_count = 0
    @classmethod
    def add_task(cls):
        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:
                    cls.task_count += 1
                    task1.write("\nUser assigned to task" + str(cls.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")

TaskCreator.add_task()

推荐阅读