I am new into learning python and I was having a hard time running a program (my code) the cmd recognizes that I have a 3.12 Python version but when I run my code which I made using VS StudioCode the file doesn't do anything. I have fixed the directory but it still remain as it is (not recognizing the python file). To be able to run my code as intended here is the code I did:
import os
from datetime import datetime, timedelta
def read_task(file1):
tasks = {}
if os.path.exists(file1):
with open(file, 'r')as file:
for line in file:
task, completed, creation_date = line.strip().split(',')
tasks[task] = {'completed': bool(int(completed)), 'creation_date': datetime.strptime(creation_date, '%Y-%m-%d')}
return task
def write_tasks(file1, tasks):
with open(file1, 'w') as file:
for task, details in task.items():
completed = details['completed']
creation_date = details[creation_date].strftime('%Y-%m-%d')
file.write (f'{task},{int(completed)},{creation_date}\n')
def main():
file1 = 'task.txt'
tasks = read_task(file1)
current_date = datetime.now()
for task, details in tasks.items():
completed = details['completed']
creation_date = details['creation_date']
if completed and (current_date - creation_date).days >= 7:
tasks[task][completed]= False
print ("Current tasks:")
completed = details['completed']
print (f'{task}: {"Done" if completed else "To do"}')
completed_task = input("Enter a completed task: ")
tasks[completed_task]['completed'] = True
write_tasks(file1, tasks)
if __name__ == "__main__":
main()
Wrong indentation for the:
if name == "main": main()
it shouldn't be under main function