Error: 'int' Object is not subscriptable

Asked

Viewed 16,467 times

-2

In a long jump competition each athlete is entitled to five jumps. The result of the athlete will be determined by the average of the five remaining values. You must make a program that receives the name and the five distances reached by the athlete in his jumps and then enter the name, the jumps and the average of the jumps. The program must be closed when the name of the athlete is not given.

You’re making a mistake on this line

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 = ""
  • 1

    You’re trying to access the position j of an integer number i when it does [i[j]]. I believe what you intend to do is [i][j], using separate brackets. Other than that, I believe it is possible to greatly improve the code.

  • 1

    Again I ask silly mistake here, I swear I research before I ask, but I found nd. How could I improve the code? I’m dumb in programming

1 answer

1

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

With this change your code seems to work as desired.

See working on Repl.it.

However, I believe that the code can be improved. This is why I answer and not only vote to close the question as a typo. Your code has many addictions that are common to people who start in Python already knowing another language - usually C. This is very clear when you use the 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 values of distances in separate lists. These are directly related data and it makes sense for you to store them together. I would use a dictionary with 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 the average calculation becomes much simpler if you use sum(...)/5. When reading the name, you don’t need to convert to string, for the return of input You’ll always be that guy now. In fact, the program does not talk about the need to store these values in a list, so the display of the name and media could be done in the same loop as the reading, not needing to create the list atletas.

See working on Repl.it.

  • Thanks for the help, I still know neither dictionaries nor this function sum(), but I will learn and I will use in my future codes. Mt thank you very much, I learned mt from you.

  • kkkkk and yes I started with C

Browser other questions tagged

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