0% found this document useful (0 votes)
3 views

Python_basic_3

This document outlines a Python-based To-Do List Application that allows users to add, view, mark, and delete tasks. It includes the complete Python code necessary for the application, detailing functions for loading, saving, and managing tasks. The main function provides a user interface for interacting with the To-Do list through a menu system.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python_basic_3

This document outlines a Python-based To-Do List Application that allows users to add, view, mark, and delete tasks. It includes the complete Python code necessary for the application, detailing functions for loading, saving, and managing tasks. The main function provides a user interface for interacting with the To-Do list through a menu system.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python To-Do List Application

Your Name
March 24, 2025

1 Introduction
This document presents a Python-based To-Do List Application. Users can add,
view, mark tasks as complete, and delete tasks.

2 Python Code
1 import json
2
3 TODO_FILE = " tasks . json "
4
5 def load_tasks () :
6 try :
7 with open ( TODO_FILE , " r " ) as file :
8 return json . load ( file )
9 except ( FileNotFoundError , json . JS O ND ec o de Er ro r ) :
10 return []
11
12 def save_tasks ( tasks ) :
13 with open ( TODO_FILE , " w " ) as file :
14 json . dump ( tasks , file , indent =4)
15
16 def add_task ( tasks ) :
17 task = input ( " Enter a new task : " ) . strip ()
18 if task :
19 tasks . append ({ " task " : task , " done " : False })
20 save_tasks ( tasks )
21 print ( f " Task ’{ task } ’ added !\ n " )
22 else :
23 print ( " Task cannot be empty !\ n " )
24
25 def view_tasks ( tasks ) :
26 if not tasks :
27 print ( " \ n No tasks available !\ n " )
28 else :
29 print ( " \ n To - Do List : " )
30 for index , task in enumerate ( tasks , start =1) :
31 status = " " if task [ " done " ] else " "
32 print ( f " { index }. { task [ ’ task ’]} [{ status }] " )
33 print ()

1
34
35 def complete_task ( tasks ) :
36 view_tasks ( tasks )
37 try :
38 task_number = int ( input ( " Enter task number to mark as
complete : " ) ) - 1
39 if 0 <= task_number < len ( tasks ) :
40 tasks [ task_number ][ " done " ] = True
41 save_tasks ( tasks )
42 print ( " Task marked as complete !\ n " )
43 else :
44 print ( " Invalid task number !\ n " )
45 except ValueError :
46 print ( " Please enter a valid number !\ n " )
47
48 def delete_task ( tasks ) :
49 view_tasks ( tasks )
50 try :
51 task_number = int ( input ( " Enter task number to delete : " ) ) -
1
52 if 0 <= task_number < len ( tasks ) :
53 removed_task = tasks . pop ( task_number )
54 save_tasks ( tasks )
55 print ( f " Task ’{ removed_task [ ’ task ’]} ’ deleted !\ n
")
56 else :
57 print ( " Invalid task number !\ n " )
58 except ValueError :
59 print ( " Please enter a valid number !\ n " )
60
61 def main () :
62 tasks = load_tasks ()
63
64 while True :
65 print ( " \ n To - Do List Menu : " )
66 print ( " 1 View Tasks " )
67 print ( " 2 Add Task " )
68 print ( " 3 Mark Task as Complete " )
69 print ( " 4 Delete Task " )
70 print ( " 5 Exit " )
71
72 choice = input ( " Choose an option (1 -5) : " ) . strip ()
73
74 if choice == " 1 " :
75 view_tasks ( tasks )
76 elif choice == " 2 " :
77 add_task ( tasks )
78 elif choice == " 3 " :
79 complete_task ( tasks )
80 elif choice == " 4 " :
81 delete_task ( tasks )
82 elif choice == " 5 " :
83 print ( " Exiting To - Do List . Have a productive day
!\ n " )
84 break
85 else :
86 print ( " Invalid option ! Please try again .\ n " )

2
87
88 if __name__ == " __main__ " :
89 main ()

You might also like