Algorithm repeating data from last append

Asked

Viewed 63 times

-2

I did this to add AGE and NAME in a Person Dict when it ends fill the data and Ubmita it sends this Dict to the People list. The problem is, all people add have the same values as the last, as I can do the fatimaneto so that it does not occur?

inserir a descrição da imagem aqui

  • 1

    Raul, please post your code.

  • 1

    Raul Pacheco, The code here in Stackoverflow should be published as text read the following link to know the reason: https://pt.meta.stackoverflow.com/a/5485/137387

1 answer

0


Good night Raul, all good?

It’s actually quite simple to sort it out.

In the part of your script when you perform 'People.append(Person)' you need to insert a function . copy(). I re-did your code in my editor, if there’s any difference, I ask you to disregard.

Note how I included '.copy()' in the line of your script where you included the '.append' function().

pessoas = []
pessoa = {}

while True:
    decisão = str(input('Você deseja adicionar uma pessoa? S/N\nSua resposta: ')).upper()

    if decisão == 'S':
        pessoa['Nome'] = str(input('Qual o nome da pessoa que deseja adicionar? '))
        pessoa['Idade'] = int(input('Qual idade da pessoa que deseja adicionar? '))

        pessoas.append(pessoa.copy())

    else:
        break
print(pessoas)

It turns out that if your script has a looping, your program will automatically be generating the question and storing the name and age, while the answer is equal to’S'. But without making a copy of what is being included, you will end up getting the same result, as you had previously.

I have tried to summarise as much as possible, if you continue with this doubt, I am available.

Good luck Raul.

Browser other questions tagged

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