Typeerror: '>' not supported between instances of 'tuple' and 'int'

Asked

Viewed 1,179 times

0

I am making a table with name, grade and situation of each student

import pandas as pd

lista_nome = list()
lista_nota = list()
index = list()

while True:
    print("=" * 10)
    nomes = str(input(f"Nome: ")).capitalize()
    lista_nome.append(nomes)

    notas = float(input("Nota do aluno: "))
    lista_nota.append(notas)

    opcao = str(input("Deseja continuar? [S/N]: ")).upper()[0]
    print("=" * 10)

    for index in enumerate(lista_nota): 
        if index > 7:
            n1 = "APROVADO"
        else:
            n1 = "REPROVADO"

    if opcao == 'N':
        break

matriz = {"Nomes": lista_nome, "Nota": lista_nota, "Situação": n1}
x = pd.DataFrame(matriz)
print(x)

When I spin it shows up:

TypeError: '>' not supported between instances of 'tuple' and 'int'

How to solve?

1 answer

1

According to the documentation, enumerate returns a sequence of tuples containing the index and its element. Then for index in enumerate(lista_nota) the variable index is a tuple, and so gives this error when trying to use it in a comparison (if index > 7).
But in case you seem to want to check just the amount, then you wouldn’t need to enumerate.

Another detail is that you should just check the notes afterward all of which were typed (i.e., outside the while). Finally, input already returns a string, so do str(input(...)) is redundant and unnecessary.

And how are you using the pandas, can create the "Situation" column using apply:

import pandas as pd

nomes = []
notas = []

while True:
    print("=" * 10)
    nomes.append(input(f"Nome: ").capitalize())
    notas.append(float(input("Nota do aluno: ")))

    print("=" * 10)
    if input("Deseja continuar? [S/N]: ").upper()[0] == 'N':
        break

df = pd.DataFrame({ "Nomes": nomes, "Nota": notas })
df['Situação'] = df.apply(lambda row: "APROVADO" if row['Nota'] > 7 else "REPROVADO", axis=1)
print(df)

I also changed the names of some variables. The lists of names and notes I named respectively nomes and notas, to give the idea that they may have more than one name and more than one note (you were using these names for the variables that only receive a name and note, and see that I deleted them, since they did not seem necessary, because they only serve to store the result of input which is then added to the list, so I did it all in one step).

After the while I check the status of each note, using apply to create the "Situation" column, whose values are based on the note.


But if I were to use the for in the list of notes, an alternative is to create another list of situations, thus:

nomes = []
notas = []
while True:
    print("=" * 10)
    nomes.append(input(f"Nome: ").capitalize())
    notas.append(float(input("Nota do aluno: ")))

    print("=" * 10)
    if input("Deseja continuar? [S/N]: ").upper()[0] == 'N':
        break

situacoes = []
for nota in notas:
    situacoes.append("APROVADO" if nota > 7 else "REPROVADO")

df = pd.DataFrame({ "Nomes": nomes, "Nota": notas, "Situação": situacoes })
print(df)

Or, if you want you can change the lines of for by a comprehensilist on:

situacoes = [ "APROVADO" if nota > 7 else "REPROVADO" for nota in notas ]

Browser other questions tagged

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