Select the two highest values of a vector and add them up?

Asked

Viewed 149 times

0

atletas = []

while True:

    nome = input("Nome: ")

    if not nome: break

    saltos = []
    for i in range(3):
        salto = float(input("Distância {}: ".format(i+1)))
        saltos.append(salto)

    atletas.append({
        "nome": nome,
        "saltos": saltos
    })

for atleta in atletas:
    print("Nome:", atleta["nome"])
    print("Saltos:", atleta["saltos"])
    print("Média:", sum(atleta["saltos"])/3)
  • Welcome to Stackoverflow in English. Start by doing Tour on the site to better understand how it works. Then avoid asking questions only with code. Try to detail your problem to the fullest so that it is clear to everyone and is easy to get help. Do not hesitate to see also how to ask

1 answer

1

You can sort your vector downwards using the method sort() and passing the argument reverse=True.

Once ordered down, you can use the function sum() to add only the first two elements of the vector, see only:

vetor = [ 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 ]
vetor.sort(reverse=True)
print sum( vetor[:2] )

Exit:

17

Browser other questions tagged

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