taskFile = "save.txt" # self explanatory
tasks = [] #might use in the future(?)
with open(taskFile, 'r') as file: #'r' mode means read
contents = file.read() #Sets contents to the contents of save.txt
def addTask():
with open(taskFile, 'w') as file: #'w' means write
file.write(contents + input("Enter your task: ") + "\n") #usually file.write just overrides the file and places something new in, but we want to add the input to the contents. :)
def completeTask():
print(contents)
delete = input("What task from these have you completed?")
for item in contents:
if delete == contents[item]:
print("Welcome to your todo list project. No idea if I'll use this in the future since its just a project.\n")
while True:
with open(taskFile, 'r') as file: #'r' mode means read
contents = file.read()
print("choose")
print("1. Add a task")
print("2. Complete a task")
print("3. View all tasks")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == "1":
addTask()
elif choice == "2":
completeTask()
elif choice == "3":
viewTask()
elif choice == "4":
break
elif choice == "5":
print("you aren't smart. choose again.")
else:
break
#๐ just wondering where to progress with the delete/complete function for the todo list project
20 messages ยท Page 1 of 1 (latest)
@remote urchin
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
im pretty sure theres a better way to do it but idk it
What does save.txt look like?
it just says "work" (I inputted that through main.py)
updated the code but i have now broken something
taskFile = "save.txt" # self explanatory
tasks = [] #might use in the future(?)
with open(taskFile, 'r') as file: #'r' mode means read
contents = file.read() #Sets contents to the contents of save.txt
tasks = contents.splitlines()
def addTask():
with open(taskFile, 'w') as file: #'w' means write
file.write(contents + input("Enter your task: ") + "\n") #usually file.write just overrides the file and places something new in, but we want to add the input to the contents. :)
def completeTask():
print(tasks)
delete = input("What task from these have you completed?")
if delete in tasks:
tasks.remove(delete)
with open(taskFile, 'w'):
file.write("\n".join(tasks))
print("Welcome to your todo list project. No idea if I'll use this in the future since its just a project.\n")
while True:
tasks = contents.splitlines()
print("choose")
print("1. Add a task")
print("2. Complete a task")
print("3. View all tasks")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == "1":
addTask()
elif choice == "2":
completeTask()
elif choice == "3":
viewTask()
elif choice == "4":
break
elif choice == "5":
print("you aren't smart. choose again.")
else:
break
There are several issue.
Let's start with 3, View all Tasks. It seems viewtask() isn't defined.
As for 1, Add a Task, Every time addTask() is run, the whole files resets due to the early reading of taskFile
In 2, Complete a Task, You are trying to run I/O (Input/Output) operations on closed files.
While your current approach is a good one, I think you should try rebuilding.
addTask()
1.1 Opensave.txtasfileinappendmode
1.1.1 Write the input of the user in thefile.
viewTask()
2.1. Opensave.txtasfileinreadmode
2.1.1. Read thefileand save a list made by splitting the contents of thefileby newlines (\n) asallTasks
2.1.2. PrintallTasks
completeTask()
3.1. Opensave.txtasfileinreadmode.
3.1.1. Read thefileand save a list made by splitting the contents of the file by newlines (\n) asallTasks
3.2. Ask user for input (item to be removed) and store inremovingTask
3.3. Check ifremovingTaskis inallTasks
3.3.1. If result isTrue, Opensave.txtasfileinwritemode.
3.3.1.1. RemoveremovingTaskfromallTasks
3.3.1.2. LetwriteStrbe''
3.3.1.3. ForiinallTasks
3.3.1.3.1. Concatenate '{i}\n' towriteStr
3.3.1.4. WritewriteStrtofile
3.3.2. If the result isFalse, Print 'Task Doesn't Exist'
Other than your file handling there shoudn't be a problem.
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.