is not accumulating values in lists

0

list1 = list ()

list2 = list ()

def read and verify ():     "" reads an integer and checks to see if this returns the read integer "" "

while True:
     try:
        inteiros= int(input("entre com numeros1 "))
        inteiros= int(input("entre com numeros2 "))
        break # se deu certo sai do laço
    except ValueError: 
        print("deu errado por favor digite novamente ")
return inteiros

for a in range (5):
    lista1[a]= lê_e_verifica  
for r in range (5):
    lista2[r]= lê_e_verifica     

Read & Check ()

"" I want the user to enter 5 numbers in list1, and 5 list 2 if he types something that does not int he should type again "" "

    
asked by anonymous 20.10.2018 / 20:43

3 answers

0

You can make your code more " pythonic using list comprehensions .

So you do not need to initialize the variables lista1 and lista2 before using them and you can assign the list values in just one line.

# Atribuindo valores para lista1 e lista2 com list comprehension
lista1 = [le_inteiro() for i in range(5)]
lista2 = [le_inteiro() for i in range(5)]

Here's what the complete code looks like:

def le_inteiro():
    while True:
        try:
            inteiro = int(input('Digite um número inteiro: '))
            return inteiro
        except ValueError:
            print('O valor informado não é válido, tente novamente.')

# Fazendo as atribuições nas listas.
print('Preencha os valores para lista 1:')
lista1 = [le_inteiro() for i in range(5)]

print('Preencha os valores para lista 2:')
lista2 = [le_inteiro() for i in range(5)]
    
22.10.2018 / 15:42
4

Hello, how are you? If you want to populate two lists with 5 integer values in each, let me make some observations in your code. I am not experienced, so if someone wants to correct me by talking nonsense, I thank: D

Your read and check function contains accents, this is not recommended, although Python does not complain. Always try to avoid these accents in definitions of functions, methods, variables, etc.
Within your function, you are typing twice on top of the same variable integers and then returning it, that is, you put the value 1 in it, and then put the value 2 by erasing the value 1. I suggest that you keep a single statement for this variable.

# Sempre deixe um espaço nas atribuições, 'a = b'.
lista1 = []
lista2 = []
# Sua função.
def le_e_verifica():
    while True:
        try:
            inteiros = int(input('Entre com o número'))
            return inteiros
        except ValueError:
            print('O valor informado não é válido, tente novamente.')
# Fazendo as atribuições nas listas.
for c in range(5):
    lista1.append(le_e_verifica())
    lista2.append(le_e_verifica())

If you want, you can for example pass a parameter to the function saying which of the lists will be filled, or something. When you try to call the function without using the parentheses, you are actually associating the function with the variable, and not calling the function as you expect, note:

# Associa a função a variável 'A'.
A = le_e_verifica
# Agora você pode chamar através de 'A'.
resultado = A()

Hope this helps you. ;)

    
21.10.2018 / 01:29
1

I do not quite understand if you want the function to return two integers or one, but the indentation is disorganized, and you're adding it in the wrong way. The correct way would be:

for i in range(5):
    lista1.append(le_e_verifica())
for j in range(5):
    lista2.append(le_e_verifica())

Oh, and avoid placing accents on function names and variables.

    
20.10.2018 / 22:05