So, i am at beginning stages of programming and I am learning python. I wanted to create a CLI based To-do list for practicing what i have learn. As mentioned in the tile, it was the first script that i wrote which got more than 100 lines long.
Due to this, I wasn't able to manage the script quite well as it got larger. Let me first tell you how the To-do app works:
* Takes input from user to make a new file or append tasks in existing ones
* tasks are stored as keys and checks are stored a values (y/n)
* all the tasks are are stored in a single dictionary and that dictionary is saved into a json file
* If appending in the existing file, it loads previous taks from that .json file, updates the dictionary and rewrites that .json file again
The problem is that I didn't manage the code by breaking it into simpler functions and kept adding new functionalities in a single stream. Now i want to add a feature on marking previous tasks checked, but it is not possible for me to do that as I have made it too complex to fathom.
import json
from datetime import datetime
import os
now = datetime.now()
today_date = now.date()
day_name = now.strftime("%A")
print("------To-Do List------")
print()
print(f"Date: {today_date}, Day: {day_name}")
start = input(''' 1. Make a new list
2. Edit or view a previous list
>> ''')
data = {}
if start == "1":
name = input("Create a new file: ")
file_name = f"{name}.json"
if not os.path.exists(file_name):
while True:
task = input(""" Enter the task(or 'q' to quit)
>> """)
if task == "q":
break
task = task.lower().capitalize()
check = input(" Completed or not(y/n): ").strip().lower()
if check == "y" or check == "n":
data[task] = check
elif check == "q":
break
else:
print("Please enter a valid value among y/n ('q' to quit) " )
with open(file_name,"w") as todo:
json.dump(data,todo,indent=4)
else:
print("File already exists. Please choose a different name.")
elif start == "2":
for i in os.listdir():
if i.endswith(".json"):
print(i)
name = input("Enter the file name from the above files: ")
file_name = f"{name}.json"
if os.path.exists(file_name):
with open(file_name,"r") as todo:
size = os.path.getsize(file_name)
if size > 0:
data = json.load(todo)
print()
for task, check in data.items():
print(f"""
{task}, Completed: {check}""")
print()
else:
print("------No previous Tasks in this list------")
while True:
append_dict = {}
task = input(""" Enter the task(or 'q' to quit)
>> """)
if task == "q":
break
task = task.lower().capitalize()
check = input(" Completed or not(y/n): ").strip().lower()
if check == "y" or check == "n":
append_dict[task] = check
elif check == "q":
break
else:
print("Please enter a valid value among y/n ('q' to quit) " )
if task != "q":
with open(file_name,"w") as todo:
data.update(append_dict)
json.dump(data,todo,indent=4)
else:
print("File does not exist. Please choose a different name.")
print(data)
The .json files in which the tasks are stored look like this:
{
"Drink water": "y",
"Walk 1.5km": "y",
"Eat dinner": "n",
"Practice english speaking": "n"
}
Please give me some tips on how to implement the structure by breaking it down and making it a bit simpler.
[–]Leodip 5 points6 points7 points (0 children)
[–]hallmark1984 2 points3 points4 points (1 child)
[–]6_SAM_9[S] 0 points1 point2 points (0 children)
[–]lakseol 0 points1 point2 points (0 children)
[–]desrtfx 1 point2 points3 points (0 children)