#๐Ÿ”’ just wondering where to progress with the delete/complete function for the todo list project

20 messages ยท Page 1 of 1 (latest)

remote urchin
#
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
unique krakenBOT
#

@remote urchin

Python help channel opened

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.

remote urchin
#

im pretty sure theres a better way to do it but idk it

jovial knot
#

What does save.txt look like?

remote urchin
#

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
jovial knot
#

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.

#
  1. addTask()
    1.1 Open save.txt as file in append mode
    1.1.1 Write the input of the user in the file.
#
  1. viewTask()
    2.1. Open save.txt as file in read mode
    2.1.1. Read the file and save a list made by splitting the contents of the file by newlines (\n) as allTasks
    2.1.2. Print allTasks
jovial knot
#
  1. completeTask()
    3.1. Open save.txt as file in read mode.
    3.1.1. Read the file and save a list made by splitting the contents of the file by newlines (\n) as allTasks
    3.2. Ask user for input (item to be removed) and store in removingTask
    3.3. Check if removingTask is in allTasks
    3.3.1. If result is True, Open save.txt as file in write mode.
    3.3.1.1. Remove removingTask from allTasks
    3.3.1.2. Let writeStr be ''
    3.3.1.3. For i in allTasks
    3.3.1.3.1. Concatenate '{i}\n' to writeStr
    3.3.1.4. Write writeStr to file
    3.3.2. If the result is False, Print 'Task Doesn't Exist'
#

Other than your file handling there shoudn't be a problem.

remote urchin
#

Thank you

#

Iโ€™ll try that when I can

unique krakenBOT
#
Python help channel closed

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.