#** I am trying to get my existing todos saved in my txt file by using the case show given in my following code:`**
while True:
user_action = input("Type add, show:")
user_action = user_action.strip()
match user_action:
case 'add':
todo = input("Enter any todo:") + "n"
file = open('todos.txt', 'r')
todos = file.readlines()
file.close()
todos.append(todo)
file = open('todos.txt', 'w')
file.writelines(todos)
file.close()
case 'show' | 'display':
for index, item in enumerate(todos):
print(f"{index + 1}. {item}")
I had already saved my existing ‘todos’ in my txt file but after running the code and using the command ‘show’, my IDE gives the following error:
Traceback (most recent call last):
File "c:\Ayan\coding\paid course.vscode\udemy.py", line 20, in <module>
for index, item in enumerate(todos):
^^^^^
NameError: name 'todos' is not defined
New contributor
Ayandeep Borkakoty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.