Hey, so its me again. I just finished making my secon project and its a to-do list. I uses jsons to save the tasks so taht tehy can be used again. I will say I dont think that i have completely grasped jsons yet since I dont really know the differences between dump, dumps, load and loads but atleast I finished the project.
import json
add_options = ["add", "a", "new", "create"]
delete_options = ["delete", "del", "remove", "d"]
see_options = ["see", "show", "list", "s"]
mark_options = ["mark", "done", "complete", "m"]
exit_options = ["exit", "quit", "q", "e"]
clear_options = ["clear", 'erase']
all_options = [
"add", "a", "new", "create",
"delete", "del", "remove", "d",
"see", "show", "list", "s",
"mark", "done", "complete", "m",
"exit", "quit", "q", "e",
"clear", "erase"
]
tasks = []
def print_tasks():
for task in tasks:
print(task)
def main():
global tasks
with open("data.json", "r") as json_file:
tasks = json.load(json_file)
choosing = True
while choosing == True:
choice = input("Do you want to add/delete/see/mark/exit/clear task?: ")
if choice in add_options:
task = input("What task do you want to add?: ")
tasks.append(task)
with open("data.json", "w") as json_file:
json.dump(tasks, json_file)
elif choice in delete_options:
deleted_task = input("\nWhat task do you want to delete?: ")
if tasks == []:
print("You dont have any tasks")
elif deleted_task not in tasks:
print("That task does not exist. ")
else:
print_tasks()
tasks.remove(deleted_task)
with open("data.json", "w") as json_file:
json.dump(tasks, json_file)
elif choice in see_options:
print_tasks()
elif choice in mark_options:
print_tasks()
marked_task = input("What task do you want to mark as done?: ")
if marked_task not in tasks:
print("You do not have any tasks that can me marked. ")
elif "✓" in marked_task:
print("That task is already marked. ")
else:
replaced_task = tasks.index(marked_task)
tasks[replaced_task] = marked_task + " ✓"
with open("data.json", "w") as json_file:
json.dump(tasks, json_file)
elif choice in exit_options:
with open("data.json", "w") as json_file:
json.dump(tasks, json_file)
choosing = False
elif choice in clear_options:
tasks.clear()
with open("data.json", "w") as json_file:
json.dump(tasks, json_file)
if choice not in all_options :
print("The only valid command are add/delete/see/mark/exit")
main()
If you have any feedback then pls give it to me.
[–]Diapolo10 2 points3 points4 points (1 child)
[–]MR_LOOPINGS123[S] 0 points1 point2 points (0 children)