Comparing list indexes in python?

Asked

Viewed 1,753 times

0

Good afternoon guys, I’m having problems with python lists...

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, he reports this mistake:

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

But the comparison becomes normal when executed directly... Could help me understand my mistake and how to fix it?

  • I did an edit to try to understand where the for and the ifs

4 answers

1

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

Something cool that you can do to avoid this kind of thing is to receive the values and turn them into float "automatically" while mounting 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 input values are reported on the same line. If this is not the case you need to do as stated in some previous answers and use the method .append()

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 method __getitem__.

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

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

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.

0

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

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 in https://repl.it/JcVB/0

0

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

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

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

Browser other questions tagged

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