0

I'm doing some python and I was making a Todo app. Everything works fine but there's an error that drives me crazy: My "add" option doesn't work. I try to enter "add" in input, but nothing happens and I just get a new line asking me to enter either add, edit, show etc...

I honestly don't understand where my mistake is, normally I'd type "add" and I'd have an option that would allow me to add a new thing to do

The code:

while True:
    user_action = input("Please right add, edit, show, complete or exit: ")
    user_action = user_action.strip()

    if "add" in user_action:
        todo = user_action[4:]

        with open("app.txt", "r") as file:
            todos = file.readlines()
        todos.append(todo)

        with open("app.txt", "w") as file:
            file.writelines(todos)

    elif "show" in user_action:
        with open("app.txt", "r") as file:
            todos = file.readlines()

        for index, item in enumerate(todos):
            item = item.strip("\n")
            row = f"{index  +1}-{item}"
            print(row)
    elif "edit" in user_action:
        number = int(user_action[5:])
        print(number)
        number = number -1

        with open("app.txt", "r") as file:
            todos = file.readlines()

        new_todo = input("Right the new sing to do please : ")
        todos[number] = new_todo + "\n"

        with open("app.txt", "w") as file:
            file.writelines(todos)

    elif "complete" in user_action:
        number = int(user_action[9:])

        with open("app.txt", "r") as file:
            todos = file.readlines()
        index = number -1
        todo_to_remove = todos[index].strip("\n")
        todos.pop(index)

        with open("app.txt", "w") as file:
            file.writelines(todos)

        message = ("Todo " + todo_to_remove + " has been completed and deleted from your list ")
        print(message)

    elif "exit" in user_action:
        break
    else :
        print("Please right one of the command indicated)")

print("Goodbye, i hope to see you soon!")
quamrana
  • 37,849
  • 12
  • 53
  • 71
Sneed5484
  • 1
  • 1
  • You need to add newlines when writing to the file. `writelines()` doesn't do this by itself, so you're writing everything on one line. – Barmar Jun 20 '23 at 15:17
  • I'm sorry but I didn't quite understand, should I modify the writelines function or do something manually with the txt file? – Sneed5484 Jun 20 '23 at 15:28
  • Did you look at the linked question? – Barmar Jun 20 '23 at 15:29

0 Answers0