How do you execute or run a python program in your desktop?

70 views Asked by At

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()
2

There are 2 answers

2
Chwaki Allan On

Wrong indentation for the:

if name == "main": main()

it shouldn't be under main function

1
Shrutika Jaiswal On

There are a few issues in your code that need to be addressed:

  1. In the read_task function, you're returning task instead of tasks.
  2. In the write_tasks function, task should be tasks.

Make sure to indent the code correctly and adjust the issues mentioned above. Also, ensure that your task.txt file exists in the same directory as your Python script.