Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Using a For loop inside a function

I'm a Python beginner trying write a program that will allow the user to input individuals names and test scores and I am supposed to give back a grade after finding the average so far I've been able to write the program but I am experiencing difficulties trying to debug it. my programs works fine until it starts to calculate the average, it usually displays an error message saying

"TypeError: 'float' object is not subscriptable" 

Could somebody please help me why my codes are not working? Thank you for your help in advance!

def calcaverage(test1,test2,test3):
    for count in range(numofstudent):
        curraverage=((test1[count]+ test2[count]+ test3[count])/3)
        if curraverage>= 90:
            grade= "A"
            return grade 
        else:
            if curraverage >= 80 and curraverage < 90:
                grade= "B"
                return grade
            else:
                if curraverage >= 70 and curraverage < 80:
                    grade= "C"
                    return grade
                else:
                    if curraverage < 70:
                        grade= "F"
                        return grade

numofstudent=int(input("How Many Students?: "))
students=[]
test1=[]
test2=[]
test3=[]
averagescore=[]
grade=[]
for count in range(numofstudent):
    currstudent=input("Please enter name of student: ")
    students.append(currstudent)
    currstudenttest1= float(input("First test score?: "))
    currstudenttest2= float(input("Second test score?: "))
    currstudenttest3= float(input("Third test score?: "))
    test1.append(currstudenttest1)
    test2.append(currstudenttest2)
    test3.append(currstudenttest3)

grade=calcaverage(test1,test2,test3)
averagescore.append(grade)
print([students], "your grade is " ,[grade])
like image 465
user3598076 Avatar asked Mar 11 '26 09:03

user3598076


1 Answers

Here comes some tough love.

There are multiple problems here. In general you need to learn to think through your program line by line, as though you were the CPU, and figure out what you want to happen at each step. Then you need to weed out mistakes until what the actual CPU does is equal to what you want.

In the second line (the for loop), the variable "curraverage" is undefined since it doesn't get defined until a couple lines later. It's the wrong variable anyway; you want to loop over all the students so you need range(numofstudent).

In the next line, "test" is undefined. You meant "test3." If you want to learn to program you just cannot allow yourself to make these kinds of mistakes.

The variable curraverge looks like a typo, but it's actually not. Think of a better name, it's not hard.

In line 5, averagescore (which was declared as a global down below, and was a list) is now re-declared as a local and bound to a float. As such you cannot append to it in line 6. Line 5 should simply be discarded, it does nothing except create a bug.

The line grade[count] doesn't do anything. You need to call grade.append to build the list of grades. But since you haven't computed the grade yet, no such line belongs here.

Next you compare averagescore to 90 and 80 and so forth, but that's the wrong variable again. That's the list you declared below. Here you want the score for one student (which is curraverage).

Then you return from inside the loop, causing the function to exit before you have computed more than one grade. None of those returns should be in there.

I could go on. You need to be much, much more careful about what you are doing. Keep in mind that computers are profoundly stupid - they do exactly what they're told, whether that's what you want them to do or not.

Best of luck to you.

like image 185
Paul Cornelius Avatar answered Mar 13 '26 22:03

Paul Cornelius