is not accumulating values in the lists

Asked

Viewed 178 times

0

list1=list()

Lista2=list()

def lê_e_verifies(): """reads an integer and checks whether this correct returns the integer read"""

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     

lê_e_verifies()

""" I want the user between 5 numbers in list1, and 5 list 2 if he type something not int he should type again"""

3 answers

4

Hey, how you doing? If you want to fill two lists with 5 integer values in each, allow me to make some remarks in your code.
I am not experienced, so if someone wants to correct me by talking nonsense, I thank :D

Its function lê_e_verifies contains accentuation, this is not recommended, although Python does not complain. Always try to avoid these accents in the definitions of functions, methods, variables, etc.
Within your function, you are writing twice on the same variable whole and then returning it, that is, you put the value 1 on it, and then put the value 2 by deleting the value 1. I suggest that you keep a single statement for that 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 telling which of the lists will be filled, or something like that.
When you try to call the function without using the parentheses, in fact you are associating the function to 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()

I hope this can help you. ;)

1

I don’t quite understand if you want the function to return two integers or one, but anyway, the indentation is disorganized, and you’re adding it wrong. The right way would be:

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

Ah, and avoid placing accents in function and variable names.

0


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

This way, you don’t need to initialize the variables lista1 and lista2 before using them and you can assign list values in only one row.

# 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)]

Follow how the full code would look:

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)]

Browser other questions tagged

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