0% found this document useful (0 votes)
37 views3 pages

Basic Code-2

This document outlines a Python-based To-Do List program that allows users to manage tasks through a command-line interface. Key functionalities include adding, viewing, marking, and removing tasks, with data stored in a JSON file. The document includes detailed Python code for implementing these features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views3 pages

Basic Code-2

This document outlines a Python-based To-Do List program that allows users to manage tasks through a command-line interface. Key functionalities include adding, viewing, marking, and removing tasks, with data stored in a JSON file. The document includes detailed Python code for implementing these features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python To-Do List Program

March 24, 2025

1 Introduction
This document presents a Python-based To-Do List program. The program
allows users to add, view, mark, and remove tasks using a simple command-line
interface.

2 Python Code
1 import json
2
3 TASKS_FILE = " tasks . json "
4
5 def load_tasks () :
6 try :
7 with open ( TASKS_FILE , " r " ) as file :
8 return json . load ( file )
9 except F i l e N o t F o u n d E r r o r :
10 return []
11 except json . J S ON De co d eE rr o r :
12 return []
13
14 def save_tasks ( tasks ) :
15 with open ( TASKS_FILE , " w " ) as file :
16 json . dump ( tasks , file , indent =4)
17
18 def show_tasks ( tasks ) :
19 if not tasks :
20 print ( " \ n No tasks available !\ n " )
21 else :
22 print ( " \ n Your To - Do List : " )
23 for i , task in enumerate ( tasks , start =1) :
24 status = " " if task [ " done " ] else " "
25 print ( f " { i }. { status } { task [ ’ task ’]} " )
26 print ()
27
28 def add_task ( tasks ) :
29 task_name = input ( " Enter task : " ) . strip ()
30 if task_name :
31 tasks . append ({ " task " : task_name , " done " : False })
32 save_tasks ( tasks )

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

2
87 else :
88 print ( " Invalid option ! Please try again .\ n " )
89
90 if __name__ == " __main__ " :
91 main ()

You might also like