Python - List Creation

Asked

Viewed 48 times

-3

I am not being able to print the data table, it is only giving the data of the first input, and I could not see where the problem is, I am beginner and making simple projects.

print ('----------------------------------------------------')
print ('                 CONTROLE DE PLANTEL                ')
print ('----------------------------------------------------')

id_do_lote = str(input('Identificação do lote:'))
numero_de_animais = (input('Número de animais no lote:'))
lista_dos_animais = []
lista_de_pesagem = []


i = 1
while i <= int(numero_de_animais):
    id_animal = str(input ('ID do animal #' + str(i)+ ' '))
    lista_dos_animais.append(id_animal)
    i += 1

i = 1
while i <= int(numero_de_animais):
    peso_animal = float(input ('Peso do animal #' + str (i)+ ' '))
    lista_de_pesagem.append(peso_animal)
    i += 1

for name_animal in lista_dos_animais:
    dados_name_animal = name_animal

for peso_animal in lista_de_pesagem:
    dados_peso_animal = peso_animal

def criarTabela ():
    print ('----------------------------------------------------')
    print ('                 Lista de dados                     ')
    print ('----------------------------------------------------')
    print ('%s:\t\t\t%f' % (dados_name_animal, dados_peso_animal))
    print('-----------------------------------------------------')

criarTabela()
  • 1

    I’m having trouble printing the data table... your program does not use tables.

  • here is the mistake dados_name_animal = name_animaland that dados_peso_animal = peso_animal

1 answer

0

The mistake is both for. Instead of adding the names and weights of the animals, you are just creating the variables and putting the values in it, that within a for then, at each iteration the previous value is replaced, and it is only the last.

In the way criarTabela() you must create a loop because you are printing a list.

dados_name_animal = []
for name_animal in lista_dos_animais:
    dados_name_animal.append(name_animal)
dados_peso_animal = []
for peso_animal in lista_de_pesagem:
    dados_peso_animal.append(peso_animal)


def criarTabela():
    print('----------------------------------------------------')
    print('                 Lista de dados                     ')
    print('----------------------------------------------------')
    for i in range(len(dados_peso_animal)):
        print(str(dados_name_animal[i]) + ':  ' + str(dados_peso_animal[i]))
        print('-----------------------------------------------------')


criarTabela()
  • Putz, that’s right Bernardo, thank you! In the part of the course I’m not appeared those range and Lens functions...

Browser other questions tagged

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