Comparing list indexes in python?

0

Good afternoon guys, I'm having trouble with lists in python ...

lateral = []
for i in range(3):
    lateral = float(input("Por favor, informe o valor de cada lado, 
                           seguidamente:" ))
if(lateral[0] == lateral[1]) and (lateral[1] == lateral[2]) and
  (lateral[0] == lateral[2]):
    print('Seu triangulo é equilatero')
elif(lateral[0] == lateral[1]) or (lateral[1] == lateral[2]) and 
    (lateral[0] == lateral[2]):
    print('Seu triangulo é isoceles')
else:
    print("Se triangulo é escaleno")

But every time I try, it reports this error:

Traceback (most recent call last):
File "/home/...../...../MODULO II/E2Q1.py", line 4, in <module>
if(lateral[0] == lateral[1]) and (lateral[1] == lateral[2]) and 
(lateral[0] == lateral[2]):
TypeError: 'float' object is not subscriptable

However, the comparison happens normal when executed directly ... Could you help me understand this error and how do I fix it?

    
asked by anonymous 20.07.2017 / 18:38

4 answers

1

This error means that the variable lateral is not a collection ( array , list , etc.). That is, it does not have an implementation of the __getitem__ method.

You declared an array at the beginning of script , but then simply reassigned this variable to a float .

I assume you intended to add user entries to the array lateral . For this, you need to add the values entered by the user using the append method.

See the corrected code:

lateral = []

for i in range(3):
    valor = float(input("Por favor, informe o valor de cada lado, seguidamente:"))
    lateral.append(valor)

if((lateral[0] == lateral[1]) and (lateral[1] == lateral[2]) and (lateral[0] == lateral[2])):
    print('Seu triangulo é equilatero')
elif(lateral[0] == lateral[1]) or (lateral[1] == lateral[2]) and (lateral[0] == lateral[2]):
    print('Seu triangulo é isoceles')
else:
    print("Se triangulo é escaleno")

See working on repl.it.

    
20.07.2017 / 18:47
0

The problem is that sidebar starts out as a list and then you turn it into a float.

To read a float and add it to the lateral list, do so:

lado = float(input("Por favor, informe o valor de cada 
       lado, seguidamente:" ))
lateral.append(lado)
    
20.07.2017 / 18:48
0

In addition to the given answers you can also receive input values in a single line.

For example:

entradas = input("Por favor, informe o valor de cada lado, seguidamente:" ).split()

lateral = list(map(float,entradas))

if(lateral[0] == lateral[1]) and (lateral[1] == lateral[2]) and (lateral[0] == lateral[2]):
    print('Seu triangulo é equilatero')
elif(lateral[0] == lateral[1]) or (lateral[1] == lateral[2]) and (lateral[0] == lateral[2]):
    print('Seu triangulo é isoceles')
else:
    print("Se triangulo é escaleno")

See working at link

    
21.07.2017 / 14:49
0

The problem is when you get the values for for loop . When you started the variable you put it as a list , however, since Python is a language that has dynamic typing, when you referenced the variable lateral to a float it does an automatic casting and loses the characteristics of a list .

Something cool you can do to avoid this kind of thing is to get the values and make them float "automatically" while moun- ting the list using something called list comprehension . The code would look like this:

lateral = [float(x) for x in input('Informe os valores seguidamente: ').split()]

if((lateral[0] == lateral[1]) and (lateral[1] == lateral[2]) and (lateral[0] == lateral[2])):
    print('Seu triangulo é equilatero')
elif(lateral[0] == lateral[1]) or (lateral[1] == lateral[2]) and (lateral[0] == lateral[2]):
    print('Seu triangulo é isoceles')
else:
    print("Se triangulo é escaleno")

The only requirement for this case is that the input values are reported on the same line. If it is not the case you need to do as stated in some previous answers and use the method .append()

    
22.07.2017 / 02:49