Error: 'int' object is not subscriptable

-2

In a distance jumping competition, each athlete is entitled to five jumps. The athlete's score will be determined by the average of the remaining five values. You must make a program that receives the name and the five distances reached by the athlete in his jumps and then inform the name, the jumps and the average of the jumps. The program must be terminated when the athlete's name is not entered.

You're giving this line error

print(nsaltos[j],todosaltos[i[j]]," m")
  

builtins.TypeError: 'int' object is not subscriptable

nsaltos = ['Primeiro Salto: ','Segundo Salto: ','Terceiro Salto: 
','Quarto Salto: ','Quinto Salto: ']
atletas = []
saltos = []
todosaltos = []
nome = 0
media = 0
lstsalto = ""
while True:
    nome = str(input("Digite o nome: "))
    atletas.append(nome)
    if nome == '':
       break
    for i in range(5):
        distancia = float(input("Digite a distancia %d: "%(i+1)))
        saltos.append(distancia)
    todosaltos.append(saltos)
    saltos = []
atletas.pop(len(atletas)-1)
for i in range(len(atletas)):
    print("Atleta: ",atletas[i])
    print("")
    for j in range(len(todosaltos[i])):
        print(nsaltos[j],todosaltos[i[j]]," m")
        media += todosaltos[i[j]]
        convsalto = str(todosaltos[i[j]])+" -"
        lstsalto += convsalto
    print("Resultado final:")
    print("Atleta: ",atletas[i])
    print("Saltos: ",lstsalto)
    print("Média dos saltos: ",(media/5)," m")
    media = 0
    lstsalto = ""
    
asked by anonymous 11.09.2017 / 02:43

1 answer

0

As I commented, the error is given in this line because you did: [i[j]] . With this code, you are accessing the j position of the i variable; as i is a variable of type int , you can not do this, generating the error. I believe you wanted to [i][j] , so access the j position of the value at the i position.

With this change your code seems to work as desired.

  

See working at Repl.it .

However, I think the code can be improved. Reason for me to respond and not only vote to close the question as a typo. Your code has many vices that are common to people starting in Python already knowing another language - usually C. This becomes very clear when you use range(len(...)) . Its code is functional, but not idiomatic, that is, it does not use the proper tools that the language offers to solve the problem.

First, I would not store the name and distance values on separate lists. They are data directly related to each other and it makes sense for you to store them together. I would use a dictionary with the keys nome and saltos to store the information and store that dictionary in a list. Something like:

atletas = []

while True:

    nome = input("Nome?")

    if not nome: break

    saltos = []
    for i in range(5):
        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"])/5)

Note that averaging is much simpler if you use sum(...)/5 . When reading the name, you do not need to convert to string , since the return of input will always be of this type already. In fact, the program does not talk about the need to store these values in a list, so the display of the name and average could be done in the same loop as the read, not needing to create the list atletas .

  

See working at Repl.it .

    
11.09.2017 / 14:51