I am learning with python update add and remove

0

Good Night I am left with homework to create a list of books where I can add delete and update the case is that I try it with a cycle for I can only update because when I try to add or delete the only thing that does is to repeat the question I established in the input or in the case when

! image with the code [] 1

This is the main one

import libros

#user = usuarios.usuario2

books = libros.ListaLibros 

for i in range(0, len(books)) :

    pregunta = input('¿Desea agregar,actualizar o eliminar un libro?')

    if (pregunta== "agregar") :
            book = input("Digite el titulo del libro que desea agregar: ")
            books.insert(0, book)

    elif (pregunta=="eliminar") :
            booksDelete =input("Digite el titulo del libro que desea eliminar: ")
            books.remove(booksDelete)

    elif (pregunta=="actualizar") :

                    print(books[i])

    else :
            print("error")

and this is the one in the books archive

libro1 = "Cocori"
libro2 = "El principito"
libro3 = "Harry Potter"
libro4 = "El señor de los anillos"
libro5   = "Los juegos del hambre"

ListaLibros = [libro1,libro2,libro4,libro5]
    
asked by Andres Latouche Ferreto 04.02.2017 в 01:30
source

1 answer

0

I would do something like this:

main.py:

import libros
books = libros.ListaLibros 
pregunta = input('¿Desea agregar,actualizar o eliminar un libro? Escriba salir para terminar')

while pregunta =! 'salir':

    if pregunta == "agregar":
        book = input("Digite el titulo del libro que desea agregar: ")
        books.insert(0, book)

    elif pregunta=="eliminar":
        booksDelete = input("Digite el titulo del libro que desea eliminar: ")
        books.remove(booksDelete)

    elif pregunta == "actualizar":
        bookActualizarOld = input("Digite el titulo del libro que desea actualizar: ")
        bookActualizarNew = input("Digite el nuevo titulo: ")
        books[books.index(bookActualizarOld)]=bookActualizarNew

    else :
        print("error")
print "Terminado!"

The books.py is as it is.

Obviously in this way the changes will not be saved, since with each execution of the script everything returns to 0.

Greetings from Argentina!

    
answered by 06.02.2017 в 18:07