Question: Python: (Please write down your plan/ programming steps) I wrote a todo list app. The problem is, a user has to start their todo list
Python: (Please write down your plan/ programming steps)
I wrote a todo list app. The problem is, a user has to start their todo list over every time they run the app, and they lose their list when they close it.
Please help me fix that.
Using my previous implementation of the todo list app (or if you arent happy with yours, you can use my solution), make the app read from and write to a file as needed to maintain the lists between sessions.
****Help me out:
call your todo file todo_app_todo.txt and your done file todo_app_done.txt. If you only use one file (which I do not recommend), you can call it todo_app_data.txt.
*****
The first time the app runs, it will have to create the files. You can check to see if its the first time the app is running by looking for each file:
import os
# exists will be True if the file is there, False if not
exists = os.path.isfile('todo_app_todo.txt')
At a minimum, your program should use the code above to look for its savefiles when it opens; and if they exist, it should read from them at that point; it should also write to the savefiles when the user chooses the option to close the file. This is absolutely sufficient for your first time managing external data for a program youre writing.
If you are feeling fancy (and if you already have that minimum solution done), a more complete solution would involve writing to the appropriate file(s) every time a change is made, in case of program crash. For extra error tolerance, the program might also read from the files every time it needs to access the lists, rather than trusting the lists in memory.
***My todo_list app Coding: Using functions to create an application to track a user's todo list. #generate a function to display contents of a list which #Will be called to print todo and completed lists
def display_list(todo_list): if len(todo_list) == 0: print(" There is nothing to display.") else: for index, item in enumerate(todo_list): print(f"{index+1}) {item}")
#generate a function to add item to to do list def add_item(todo_list): add = input(" What would you like to add? ") todo_list.append(add)
#generate a function to mark to do item as completed def mark_item_complete(todo_list,completed_list): print("Choose an item to mark completed. ") display_list(todo_list) i = int(input(" Please choose one of the options above: ")) completed_list.append(todo_list.pop(i-1)) #generate the main function to get everything in def main(): #initialize the variables and lists todo_list = [] completed_list = [] choice = None
#greeting the user print("Welcome to the Todo List Application. ") #keep loop going until user wants to end the program while(choice!=5): #display the menu of choice to the user print(" 1) View the items on your todo list") print("2) View the items you have finished") print("3) Add an item to the todo list") print("4) Mark a todo list item as completed") print("5) Exit the Todo List Application (your list will not be saved) ") choice = int(input("Please choose one of the options above: ")) if choice == 1: print(" This is your todo list:") display_list(todo_list) elif choice == 2: print(" This is your list of completed tasks.") display_list(completed_list) elif choice == 3: add_item(todo_list) continue elif choice == 4: mark_item_complete(todo_list,completed_list) continue elif choice == 5: break else: # validate the user's input print('Not a valid option. Try again. ') input(" Hit enter when finished viewing ") print(" Thank you for using the Todo List Application. ")
# the incantation to make main() go if __name__ == "__main__": main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
