Home > Back-end >  Trying to use append user input into list
Trying to use append user input into list

Time:01-25

I'm learning python at the moment so i am facing following error in my code please help me to address this isssue.

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

    match user_action:
        case 'add':
            todo = input('Enter new todo')
            file = open('todos.txt', 'r')
            todos = file.readline()
            file.close()


            todos.append(todo)

            file = open('todos.txt', 'w')
            file.writelines(todos)
            file.close()

        case 'show':
            for index, item in enumerate(todos):
                row = f"{index   1}-{item.title()}"
                print(row)
        case 'edit':
            number = int(input("Enter the number you want to edit:"))
            number = number - 1
            new_todo = input("Enter new list:")
            todos[number] = new_todo
        case 'complete':
            number = int(input("Enter what number from the list want to remove:"))
            todos.pop(number - 1)
        case 'exit':
            print('Goodbye see you later!')
            break

code above showing the following error.

`Traceback (most recent call last):
  File "H:\python\app1\main.py", line 14, in <module>
    todos.append(todo)
AttributeError: 'str' object has no attribute 'append'`

I want to add user input in the list

CodePudding user response:

try using the following:

todos = todos ' ' todo

CodePudding user response:

You are using readline(), which returns thr first line of the file. Consider using readlines() instead, which returns a list of all of the lines in the file.

Example:

todos = file.readlines()
  • Related