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).
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– Lucas