From what I understand, you want to implement a script that is able to mount a dicionário
containing nome
and altura
of 10 people and then display the name of the tallest person.
Well, you can use the following script:
# Montando um dicionário com nome e altura de 10 pessoas.
people = dict()
for c in range(1, 11):
n = input(f'{c}º Nome: ').strip().title()
a = float(input(f'Altura de {n}: '))
people.update({n: a})
# Verificando a maior altura.
maior = people[max(people, key=people.get)]
print('\033[32mPessoa(s) de maior(es) altura(s):')
for n, a in people.items():
if a == maior:
print(f'{n}')
Note that when we run this script the 1st block for. It is through it that the respective dictionary will be assembled.
So when we run the script we get the following message: 1º Nome:
. At this point we must enter the first name and press enter
. Then we received the following message: Altura de fulano:
. Right now we should type the height of the said guy and press enter
. Next we must enter the name and height of other people.
After typing name and height of the last person - 10th person - and pressing enter
, the 2nd block for. At this time, the highest height and the name(s) of the(s) person(s) who owns it shall be verified. The name(s) of the highest person(s) (s) is displayed next.
Observing
THE PRIORITY FUNCTION OF A LOOP OF REPETITION - IN THIS CASE FOR - IS TO REPEAT INTERACTIONS. Well, if you hadn’t inserted the 2nd FOR only one person would be shown who, perhaps, had the highest height. How to insert the 2nd FOR the code will be able to display THE NAMES OF ALL PEOPLE which may have the maximum height.
So the use of this 2nd FOR and HIGHLY NEEDED.
'Cause if you pay attention to penultimate print()...
print('\033[32mPessoa(s) de maior(es) altura(s):')
...refers to cases where there is only one way out - one person’s name - or more exits - several people.
Searching the Stack Overflow I found a question that has the answer for what you need, follow link: https://answall.com/questions/382490/python-maior-valor-de-um-dicion%C3%A1rio-por-chave
– Vitor Subhi
Just a detail, in doing
float(input(...))
, the result is already afloat
, then you don’t need to check if thetype
of the variable isfloat
. If a valid number is not entered, it launches theValueError
and the code falls right intoexcept
, so this oneif
it’s unnecessary, you could call thebreak
direct: https://ideone.com/7Ah4Ap– hkotsubo