Please forgive me if this seems an easy fix I am early on in my coding path, and thank you in advance for any help.
I am trying to access the due date (element 3 in the list below) and cross check it with todays date to see if the task is overdue.
This is the file that the list is in (tasks.txt): This is read as - user task assigned to; What needs doing; description of task; Due date; When task was added; is task complete.
sam;needs doing;now;2004-04-12;2024-03-05;No
sam;please do;gym;2024-05-20;2024-03-05;No
admin;do;sd;2004-12-12;2024-03-05;No
dam;dam;dam;2024-12-12;2024-03-05;No
The code I have put together so far is:
elif menu == 'gr':
# creating dictionary with values for task_overview file
stat_dict_task_overview = {
"Tasks": 0,
"Completed": 0,
"Uncompleted": 0,
"Overdue": 0,
"Percentage Incomplete": 0,
"Percentage Overdue": 0
}
# creating dictionary with values for user_overview file
stat_dict_user_overview = {
"Users": 0,
"Tasks": 0,
"Name": 0,
}
# open tasks.txt as read
tasks = open("tasks.txt", "r")
# Split contents task.txt by ';'
for count, line in enumerate(tasks):
split_by_semi = line.split(';')
# unpack elements in task.txt into corresponding variables
user = split_by_semi[0]
how_many_tasks = split_by_semi[1]
description_task = split_by_semi[2]
due_date = split_by_semi[3]
date_added = split_by_semi[4]
completed = split_by_semi[5]
# Add 1 to "Tasks" for each line
stat_dict_task_overview["Tasks"] += count + 1
# Add 1 to "Completed if "Yes"
if[5] == "Yes":
stat_dict_task_overview["Completed"] += count + 1
# Else add 1 to "Uncompleted"
else:
stat_dict_task_overview["Uncompleted"] += count + 1
# Code stuck on needs to check todays date against due date and add 1 if overdue
if[3] < datetime.today().date():
stat_dict_task_overview["Overdue"] += count + 1
# Print results for my ongoing review
print(stat_dict_task_overview)
I think your code has several problems.
First of all, the syntax
if [5] == 'Yes':will be alwaysFalsebecause you are comparing a string ('Yes') with a list ([5]).Second, if you use the variables you defined in the loop, you are going to use only the very last line of your file.
Third, more tricky, the line you are going to use will probably need to be stripped, to remove the trailing newline.
Fourth, the logic of your code is not very clear: if you want to count stuff using the dict, the increment should be
+= 1and not+= count + 1.Finally, for the problem with the date comparison, you should use this code:
But probably, your entire code should be: