How to delete an element from an Array String?

Asked

Viewed 897 times

1

I’m having the following build error:

del(alimentos[i])  
TypeError: 'str' object doesn't support item deletion

What did I do wrong? I would like to remove the typed item from the list.

My code:

dieta = []
alimentos = ""
comer = ""

while len(dieta) is not 26:
    alimentos = input("Qual o alimento? ")
    if alimentos == "fim":
        break
    dieta.append(alimentos)

i = 0
while i <= len(dieta):
    comer = input("Qual alimento deseja comer? ")
    if comer in dieta:
         print("pode comer")
         del(alimentos[i])
    i += 1

2 answers

2


Your code is a little wrong.

He’s deleting according to the i, but if I don’t want to delete the first element? ( 0 ) and yes the second ( 1 )? After all, he deletes what I "eat," right?

And also the error occurs because you are trying to take an element out of a string, not a list.

I changed a little bit and left like this :

You also do not need to declare the variable alimentos nor comer at first.

Can by While 1: or While True: for it to run until you type "end".

dieta = []

while 1: # Infinito
    alimentos = input("Qual o alimento? Digite 'fim' para encerrar")
    if alimentos == "fim":
        break
    else:
        dieta.append(alimentos)

while 1: # Infinito
    if len(dieta) == 0: # Se a lista esvaziar, ele encerra, senão, ele continua.
        print('Acabou a lista.')
        break
    else:
        print(dieta)
        comer = input("Qual alimento deseja comer? ")
        if comer in dieta:
            print("pode comer")
            deletar = dieta.index(comer) # Posição do item que quer deletar.
            del(dieta[deletar])
        else:
            print('Não pode comer')
  • Error in your Antony code. Check print: http://imgur.com/a/6O7gR

  • 1

    I don’t know what went wrong there. I just tested it here.

  • The code posted here is really ok, take a look at it running here https://repl.it/HwO4/0

  • For me keeps giving the error... Nameerror: name 'fish' is not defined, as if I had to define the variable fish that is my input '-'

  • Are you using my code or are you using yours with some modifications based on mine? If it is the second option try using my code only and see what happens. If it is not the case, I do not know what it is, because here it is quiet.

  • I’m using your code. I’d also like to know the problem, because it doesn’t make sense.

  • Thanks guys, I managed to solve the problem: I updated Python from my PC from 2.7 to 3.6. And I uninstalled Pycharm, deleting everything in the old one, and installed again the latest version by selecting interpreter 3.6 on Pycharm.

Show 2 more comments

0

You can use the remove if it has more than an equal value in the list, it removes the first occurrence found, I did a test with just a while

def cadastrar():
    dieta = []

    while True:
        if(dieta):
            print(dieta)    
        alimento = input("'fim' para encerrar \n'consumir' para comer \nQual o alimento: ").lower()
        if alimento == 'fim':
            break
        elif alimento == 'consumir':
            if(dieta):
                print(dieta)
                comer = input("Digite o alimento para consumir : ").lower()
                if comer in dieta:
                    print("pode comer")
                    dieta.remove(comer)
                else:
                    print("Não pode comer")
            else:
                print('Não tem alimentos para comer')
        else:
            dieta.append(alimento)
    return dieta

Browser other questions tagged

You are not signed in. Login or sign up in order to post.