Muroxxas
Muroxxas

Reputation: 157

Why am I unable to increment a cell in my array?

A project I'm working on requires me to determine what range a grade falls in, increment a specific cell in an array, and then use that array to print out a histogram to the user in plain text. Currently, whenever my code reaches the point where it must increment the value of a cell in the array, I receive the error:

Traceback (most recent call last):
    addGrade(gradeInput)
  line 13, in addGrade
    arrayOfGrades[index]=arrayOfGrades[index]+1
TypeError: 'set' object does not support indexing

I'm pretty sure that it will appear in the other spots where I attempt to use the arrays as well. Here's the relevant code.

gradeInput=0
arrayOfGrades={0,0,0,0,0,0,0,0,0,0,0}
i=10
def addGrade(Grade):
    global arrayOfGrades
    index=int(Grade/10)
    arrayOfGrades[index]=arrayOfGrades[index]+1

Right there, I should be seeing the cell increment, but I'm getting the error.

while gradeInput!=SENTINEL:
    gradeInput=float(input('Please enter your grade, or enter -1 to stop.'))
    while gradeInput >100 or gradeInput <SENTINEL or (gradeInput>SENTINEL and gradeInput<0):
        gradeInput=float(input('Invalid grade. Please enter a number between 0-100 '
                               'for your grade, or enter -1 to quit.'))

    if gradeInput==SENTINEL:
        print("All grades entered.")
    else:
        addGrade(gradeInput)

And I expect it to happen at the bottom of that code segment as well.

for x in range(10):
     printGradeCount(i,arrayOfGrades[i])
     i=i-1

And here, too.

Upvotes: 0

Views: 46

Answers (1)

Simba
Simba

Reputation: 1651

arrayOfGrades={0,0,0,0,0,0,0,0,0,0,0}

The {...} creates a set data structure, which cannot be indexed. What you seem to want is a list which uses the [...] syntax.

Try changing it to:

arrayOfGrades=[0,0,0,0,0,0,0,0,0,0,0]

Upvotes: 2

Related Questions