Len and Max Float together Python

Asked

Viewed 49 times

0

I’m trying to figure out the index of the highest number in the list and add the index number to a variable, but it’s giving an error of float, which list cannot be executed with float. How can I fix this?

nome = 0
altura = 0
alturas = []
nomes = []
quant = 0

while True:
    nome = input('Nome da moça: ').upper()
    if nome == 'FIM':
        break
    elif nome != 'FIM':
        nomes.append(nome)
        altura = input('Altura da moça: ')
        alturas.append(float(altura))
        quant += +1
maior = len(max(alturas))
  • 1

    You don’t need the len. length (fulfillment) is a function defined for collections (how many elements have a list, how many values have a tuple, etc.). A float is just a number

2 answers

3

the len serves for you to get the list size and not the index of a value. What you want to do is find the maximum index, but you need to be careful because there can be more than one maximum (two heights with the same value). If you are only interested in the first appearance of the maximum can use the lista.index thus:

maior = alturas.index(max(alturas))

if you want to ask everyone can do so:

maximo = max(alturas)
maiores = [indice for indice, valor in enumerate(alturas) if valor == maximo]

1

If you have any list (not necessarily typed by the user), you can use the another answer.

But in your specific case, where the user is typing the data, you can already calculate everything in the same loop who reads the information:

alturas = []
nomes = []
maior = float('-inf')
i = 0
while True:
    nome = input('Nome da moça: ').upper()
    if nome == 'FIM':
        break
    nomes.append(nome)
    altura = float(input('Altura da moça: '))
    if altura > maior:
        maior = altura
        indice_maior = i
    alturas.append(altura)
    i += 1

print(f'Maior altura é {maior}, e está no índice {indice_maior}')

Note that you don’t need to create variables nome and altura before the while, with any artificial value. They can be created within the loop, only when necessary.

And the if nome == 'FIM' doesn’t need else. For if you enter the if he calls break, coming out of while (i.e., it will not perform the rest). And if it does not enter the if, is because it was definitely not typed "END", so a elif to test this again is redundant.

The idea is that, as the values are being read, I already check if it is the largest, and also keep the respective index.


But we also have to look at the tie cases, as indicated in the other answer. Another detail is that if the names and heights are related, it might be better to keep them together instead of having separate lists. One option is to create tuples containing the name and height:

maior = float('-inf')
dados = []
while True:
    nome = input('Nome da moça: ').upper()
    if nome == 'FIM':
        break
    altura = float(input('Altura da moça: '))
    if altura > maior:
        maior = altura
    dados.append((nome, altura)) # insere uma tupla contendo o nome e altura

# ou no caso de empate
print(f'Pessoas mais altas, com {maior} de altura:')
for i, (nome, altura) in enumerate(dados):
    if altura == maior:
        print(f'índice {i} = {nome}')

Note that now you only have one list, containing all the data. And each element is a tuple, containing the name and height.

To insert I did dados.append((nome, altura)). It may seem like there’s too many parentheses, but no: if I just did dados.append(nome, altura), would error because so I am passing 2 parameters to append, that only accepts one. By placing another pair of parentheses, I am saying that I want to insert the tuple (nome, altura) on the list.

In the while I keep checking the largest as I read the data, and then I go through the list and print out the names and indexes of the tallest people (so, in case of a tie, you have all).

Browser other questions tagged

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