[Python] Error_message: The truth value of an array with more than one element is ambiguous....

Discussão em 'Python' iniciado por Stack, Maio 24, 2025.

  1. Stack

    Stack Membro Participativo

    I'm having a problem with my code: when I make a search in the i-th column of the matrix, I get the following error:

    if grades[:,i]!=-3:


    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    ​

    I have tried to read the older posts on this issue, but can't really relate to my problem

    I am trying to program a grading function according to danish grading-scale

    def computeFinalGrades(grades):
    #looping over the leth of grades
    for i in range(len(loopingrange)):
    #checking if any grade in the i-th column is -3
    if grades[:,i]==-3:
    #if a grade is equal to -3 then the output variable gradesFinal is -3
    gradesFinal = -3
    else:
    #looping over all grade columns that do not contain one or more grades of -3
    for i in range(len(loopingrange)):
    #Checking to see if any column only contains 1 grade
    if (len(grades[:,i]) == 1):
    # if a column only contains 1 grade - that is also the final grade
    gradesFinal = grades[:,i]
    #Checking to see if column contains more than 1 grade and those grades are not -3
    elif (len(grades[:,i]) > 1):
    #storing all the grades in a variable - in random order
    unsortedgrades = grades[:,i]
    #sorting all the grades from lowest to highest
    sortedgrades1 = np.sort(unsortedgrades)
    #slicing the lowest grade out using indexing
    sortedgrades = sortedgrades1[1::]
    #finding the final grade as the mean of the n-1 grades
    gradesFinal = np.mean(sortedgrades)
    return gradesFinal

    Continue reading...

Compartilhe esta Página