Python 返回主菜单\n“”) 如果editOption==“e”: 如果任务[5]=“是”: 打印(“任务已完成,不允许进一步更改”) 其他: 编辑=输入(“”)您想编辑什么: u-用户名 d-截止日期\n“”) 如果编辑==“u”: 任务[0]=输入(“请输入新用户:”) f、 写入(任务[0]) 其他: 任务[3]=输入(“请输入新的截止日期(dd-MMM-yyyy):”) f、 写入(任务[3]) elif editOption==“c”: 任务[5]=输入(“如果任务已完成,请键入‘是’:) f、 写入(任务[5]) #elif editOption==“-1” f、 关闭() #===================查看我的任务(vm)====== 如果选择==“vm”: 视图_mine() **task.txt文件:*(每个任务在一行中) 管理员,使用taskManager.py注册用户,使用taskManager.py添加将使用此程序的所有团队成员的用户名和密码,2019年10月10日,2019年10月20日,否 管理员,分配初始任务,使用taskManager.py为每个团队成员分配适当的任务,2019年10月10日,2019年10月25日,编号 abc,分配初始任务,使用taskManager.py为每个团队成员分配适当的任务,2019年10月10日,2019年10月25日,编号 **user.txt** 管理员,adm1n abc,defg

Python 返回主菜单\n“”) 如果editOption==“e”: 如果任务[5]=“是”: 打印(“任务已完成,不允许进一步更改”) 其他: 编辑=输入(“”)您想编辑什么: u-用户名 d-截止日期\n“”) 如果编辑==“u”: 任务[0]=输入(“请输入新用户:”) f、 写入(任务[0]) 其他: 任务[3]=输入(“请输入新的截止日期(dd-MMM-yyyy):”) f、 写入(任务[3]) elif editOption==“c”: 任务[5]=输入(“如果任务已完成,请键入‘是’:) f、 写入(任务[5]) #elif editOption==“-1” f、 关闭() #===================查看我的任务(vm)====== 如果选择==“vm”: 视图_mine() **task.txt文件:*(每个任务在一行中) 管理员,使用taskManager.py注册用户,使用taskManager.py添加将使用此程序的所有团队成员的用户名和密码,2019年10月10日,2019年10月20日,否 管理员,分配初始任务,使用taskManager.py为每个团队成员分配适当的任务,2019年10月10日,2019年10月25日,编号 abc,分配初始任务,使用taskManager.py为每个团队成员分配适当的任务,2019年10月10日,2019年10月25日,编号 **user.txt** 管理员,adm1n abc,defg,python,function,Python,Function,更新文件时,更安全的方法是创建一个临时文件,在其中写入内容,同时保持原始文件的安全和不受影响。然后,当写入过程完成时,可以删除原始文件并重命名临时文件 另外,我认为您应该有某种唯一的ID来标识每个任务。我必须使用行号来标识它们,但是如果您包含一些不可变的ID,那就更好了 最后,我建议您在获取任务时使用字典而不是列表。它允许您更轻松地访问和更新字段 (在下面的例子中,我没有包括所有的菜单选项,我只包括用户名编辑来说明它应该如何工作) @非常感谢!! > #This program is a

更新文件时,更安全的方法是创建一个临时文件,在其中写入内容,同时保持原始文件的安全和不受影响。然后,当写入过程完成时,可以删除原始文件并重命名临时文件

另外,我认为您应该有某种唯一的ID来标识每个任务。我必须使用行号来标识它们,但是如果您包含一些不可变的ID,那就更好了

最后,我建议您在获取任务时使用字典而不是列表。它允许您更轻松地访问和更新字段

(在下面的例子中,我没有包括所有的菜单选项,我只包括用户名编辑来说明它应该如何工作)


@非常感谢!!
> #This program is a task manager for a small business and allows the user
#to add and assign tasks to users, as well as add new users


#======== User Login ====================

#read the use.txt
username_reg = []
password_reg = []
username_list = []
password_list = []

with open('user.txt', 'r+') as f:           #open user.txt
    for line in f:                          #Now we reading user.txt
        line = line.replace(" ", "")        #replace space before password with no space
        line = line.replace("\n", "")       #remove the next line character as this will take a space in the list
        line = line.split(",")              #separate user name and password
        username_reg = line[0]
        password_reg = line[1]
        username_list.append(username_reg)
        password_list.append(password_reg)

print(username_list)
print(password_list)
print(len(username_list))
username = input("Please enter username: ")
password = input("Please enter password: ")


i = 0; login = 0 #Here login is like a flag. Which get the value 1 once the login is successful else stays 0.
while i < len(username_list):
    if username == str(username_list[i]) and password == str(password_list[i]):
        login = 1
        if username == "admin":
            selection = input('''Please select one of the following options:
                             r - register user
                             a - add task
                             va - view all tasks
                             vm - view my tasks
                             gr - generate reports
                             ds - display statistics
                             e - exit\n''')
        else:
            selection = input('''Please select one of the following options:
                             a - add task
                             va - view all tasks
                             vm - view my tasks
                             e - exit\n''')
            
        break
    i+=1
    if i==len(username_list) and login == 0:
        print("invalid username or password")   
        username = input("Please enter username: ")
        password = input("Please enter password: ")
        i = 0

##=====================View My Tasks Function====================
def view_mine():
    num_task_list = []  #This is the list for the Task Numbers
    f = open('tasks.txt', 'r+')
    row = f.readlines()
    num_task = 0
    for i in row:
        task = i.replace(" ", "")
        task = i.replace("\n","")
        task = i.split(",")
        num_task +=1
        num_task_list.append(num_task)
        if username == task[0]:
            sentence = (f'''
                            Task Number     : {num_task}
                            Task assigned to: {task[0]}
                            Task title      : {task[1]}
                            Task descrition : {task[2]}
                            Due Date        : {task[3]}
                            Date Assigned   : {task[4]}
                            Completed       : {task[5]}\n''')
            print(sentence)
    
#This part of the code is supposed to ask the user(only the user that is logged in) which task they would like to edit
#They would select a number, then I would
    TaskNum = int(input("Please select Task Number you would like to edit: "))  #user inputs the task they would like to edit
    for j in range(0, len(num_task_list),1):
        
        if TaskNum == num_task_list[j]:    #This checks the task number against the list for task numbers
            editOption = input('''Would you like to:
                                   e - edit task
                                   c - mark complete
                                   -1- return to main menu\n''')

            if editOption == "e":
                if task[5]=="yes":
                    print("Task completed, no further changes allowed")
                else:
                    edit = input('''What would you like to edit:
                                             u - username
                                             d - due date\n''')
                    if edit == "u":
                        task[0] = input("Please input new user: ")
                        f.write(task[0])
                                
                    else:
                        task[3] = input("Please in put new due date (dd MMM yyyy):")
                        f.write(task[3])
                        
            elif editOption == "c":
                        task[5] = input("Please type 'Yes' if task is completed: ")
                        f.write(task[5])    
            #elif editOption == "-1"

    f.close()  
#===============view my tasks (vm)======
if selection == "vm":
    view_mine()

**task.txt file:**(Each task is in one line)
admin, Register Users with taskManager.py, Use taskManager.py to add the usernames and passwords for all team members that will be using this program., 10 Oct 2019, 20 Oct 2019, No
admin, Assign initial tasks, Use taskManager.py to assign each team member with appropriate tasks, 10 Oct 2019, 25 Oct 2019, No
abc, Assign initial tasks, Use taskManager.py to assign each team member with appropriate tasks, 10 Oct 2019, 25 Oct 2019, No

**user.txt**
admin, adm1n
abc, defg
import os
from pprint import pprint

# The view_mine function should receive the username as a parameter
def view_mine(username):
    tasks = []
    i = 0
    with open('tasks.txt') as f:
        lines = f.read().splitlines()
    for db_row, line in enumerate(lines):
        assigned_to, *rest = line.split(', ')
        if username == assigned_to:
            # use a dictionary to easily refer to the taks' fields
            data = {k: v for k, v in zip(
                ('number', 'db_row', 'assigned_to', 'title', 'description',
                 'due_date', 'date_assigned', 'completed'),
                (i + 1, db_row, assigned_to, *rest))}
            tasks.append(data)
            i += 1
    # You can customize what you want to print, I just used pprint as a shortcut for this example
    pprint(tasks)
    task_num = int(input("Please select Task Number you would like to edit: "))
    # Get specific task at given index
    task = tasks[task_num - 1]
    edit_option = input('''Would you like to:
                                   e - edit task
                                   c - mark complete
                                   -1- return to main menu\n''')
    if edit_option == 'e':
        # This is how you would refer to the fields
        if task['completed'] == 'No':
            edit = input('''What would you like to edit:
                                             u - username
                                             d - due date\n''')
            if edit == "u":
                        # updating a field
                        task['assigned_to'] = input("Please input new user: ")

    # Actual file update part
    fetched_rows = [task['db_row'] for task in tasks]
    with open('tasks.txt') as f, open('temp.txt', 'w') as t:
        for db_row, line in enumerate(f):
            if db_row in fetched_rows:
                fetched_rows.remove(db_row)
                print(', '.join(v for k, v in list(tasks.pop(0).items())[2:]), file=t)
            else:
                print(line.strip(), file=t)
    
    os.remove('tasks.txt')
    os.rename('temp.txt', 'tasks.txt')