Python 循环浏览任务编号
有人能帮我修改一下当前的代码吗。我想向保存在输出文本文档中的任务添加任务编号。我需要循环它,这样每个任务都会被分配下一个任务编号。如果可能的话,我希望以后能够调用这些任务编号 到目前为止,我的代码是:Python 循环浏览任务编号,python,for-loop,numbers,external-data-source,Python,For Loop,Numbers,External Data Source,有人能帮我修改一下当前的代码吗。我想向保存在输出文本文档中的任务添加任务编号。我需要循环它,这样每个任务都会被分配下一个任务编号。如果可能的话,我希望以后能够调用这些任务编号 到目前为止,我的代码是: def add_task(): if menu == "a" or menu == "A": with open( 'user.txt' ) as fin : usernames = [i.split(',')[0] for i in fin.readlines(
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()
首先,我不想详细介绍,但是您存储输出的方式效率很低,而且很难访问文本文件越大的内容。为什么不使用一些免费的数据库系统来存储数据呢 其次。假设你想一次写多个任务,但只能“保存”一次,那么,考虑使用DICT的DICT。< /P>
def write_task_to_txt(task):
### break down the dict to from your lines to write to your text
def add_task(task_list,task_id):
new_tasks[task_id] = {}
new_tasks[task_id]["username"] = "username"
new_tasks[task_id]["title"] = "Task 1"
### How you fill up a task
return task_list
new_tasks = {}
for i in range(10):
new_tasks = add_task(new_tasks,i+1)
write_task_to_txt(new_tasks)
使用此功能,您可以始终使用new_tasks[task_id]访问任务,以提取该任务的所有数据。注意for循环使用的是迭代器。如果要避免这种情况,可以使用全局循环和while循环。但如果您想这样做,我建议您将应用程序转换为类并使用类变量
下面是我将如何尝试的一个框架:
class yourclass():
def __init__(self):
self.task_num = 1 #use 1 if no values
self.tasks_towrite = {}
self.mode_select()
def mode_select(self):
self.menu = input("choose mode")
while(1):
if self.menu == "a" or self.menu == "A":
self.add_task()
if self.menu == "s".casefold() #Cool function that does the same as your menu thingy
self.write_to_text()
else:
print("exit")
self.close_program()
def close_program(self): # Exit function
print("exiting")
sys.exit(1)
def add_task(self): #Add task
with open( 'user.txt' ) as fin :
self.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 self.usernames :
task = input("Username not registered. Please enter a valid username.\n")
else:
new_task = {}
new_task["username"] = task
new_task["title"] = input("Please enter the title of the task.\n")
new_task["description"] = input("Please enter the task description.\n")
new_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:
new_task["completed"] = "No"
else:
new_task["completed"] = "Yes"
new_task["assigned"] = date
self.tasks_towrite[self.task_num] = new_task
sefl.task_num +=1 #New test number
return None #returns to mode_select
def write_to_text(self):
with open('tasks.txt', 'a') as task1:
for i in self.tasks_towrite:
task1.write(str(i) + "\n") #Writes it all at once You can reformat however you want
self.tasks_towrite = {}
print("The new assigned tasks has been saved")
return None #returns to menu or you could go to self.close_program to exit
if __name__== '__main__':
x = yourclass()
我不知道为什么,但在输入到期日后,它会循环返回并再次要求输入用户名??有什么想法吗?谢谢你的帮助。你是什么意思?哪个代码示例?第一还是第二?无论哪种方式,我的两个代码示例都需要再次运行add_任务的完整过程,这是您要求的,对吗?否则,您可以简单地添加另一个输入->if else,在while循环中将任务添加到同一用户名,并使用break退出。