'how can I fix this bug : 'numpy.ndarray' object has no attribute 'append'
I'm trying to write a function to calculate the GPA of a student but I have this bug 'numpy.ndarray' object has no attribute 'append' . I want to put AVGPA inside if sid in student.SID so that I could link the GPA to its student.
Could anyone help me with this one ? Thank you
stumarks = []
class student:
def __init__(self,id,name,DOB):
self.id = id
self.name = name
self.DOB = DOB
student.avgGPA = None
class marks_object:
def __init__(self,CID, SID,SN, m,crd):
self.CID = CID
self.SID = SID
self.SN = SN
self.m = m
self.crd = crd
def MarkInput():
CourseID = input("Enter the course's ID : ")
credit = crd_of_course()
if CourseID not in [CourseInfo.id for CourseInfo in courselist]:
print(" The course's id isn't founded! Please try again!")
else:
nm = int(input("Number of student that you want to enter marks: "))
for i in range(nm):
while True:
StuID = input("Enter a student's ID : ")
SN = input("Enter the student's name : ")
if StuID not in [StudentInfo.id for StudentInfo in studentlist]:
print("The student's ID isn't founded! Please try again! ")
continue
if SN not in [StudentInfo.name for StudentInfo in studentlist]:
print("The student's name isn't founded! Please try again! ")
continue
break
marks = RoundDown()
obj = marks_object(CourseID,StuID,SN,marks,credit)
stumarks.append(obj)
def avgGPA():
sid = input("Enter the student's ID : ")
coursecredit = []
coursemark = []
for student in stumarks:
if sid in student.SID:
coursecredit.append(student.crd) # I HAVE THE BUG IN THIS LINE
coursemark.append(student.m)
coursecredit = np.array(coursecredit)
coursemark = np.array(coursemark)
output = np.sum(np.multiply(coursecredit,coursemark))
sumcrd = np.sum(coursecredit)
AVGPA= output/sumcrd
student.avgGPA = round(AVGPA,1)
print(f"The student's GPA is {round(AVGPA,1)}")
Solution 1:[1]
append method works when coursecredit is [], a list. It fails when it is an array in the next iteration.
coursecredit = []
coursemark = []
for student in stumarks:
if sid in student.SID:
coursecredit.append(student.crd) # I HAVE THE BUG IN THIS LINE
coursemark.append(student.m)
coursecredit = np.array(coursecredit)
Why did you think this would work? The error should have sent you searching numpy for an append method, and reviewing basic Python for list append. There is a numpy.append function, but its usage is different, and behavior different as well. Stay away from it.
At least you noted where the error occurred, though it's easy to miss. Showing the full error message would have been better.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hpaulj |
