I am good in python. I have just started to learn classes and I have tried to make a class. It does not seem to work though, it gives me errors every time. The code is suppose to calculate students averages. Sorry if a lot of the code is wrong, this is a dry run for me... TY
My Code:
class studentgrades:
def __init__(self,grades,num_of_grades,grades_total):
self.grades = []
self.num_of_grades = num_of_grades
self.num_of_grades = 0
self.grades_total = grades_total
self.grades_total = 0
def studentaverage(self,average):
self.average = average
for i in grades:
self.num_of_grades += 1
for i in grades:
self.grades_total += i
self.average = self.grades_total / self.num_of_grades
student1 = studentgrades(100,0,0)
print student1.studentaverage
Here is my error message:
<bound method studentgrades.studentaverage of <__main__.studentgrades instant at 0x0229CE90>>
3 Answers 3
First, you're initializing self variables to an empty list, and 0s when they are arguments of your __init__ method. This essentially voids any arguments you pass the class by immediately resetting them to nothing.
Also, to call the method of a class, pass it the number of arguments it requires. If you're calling a method with no arguments, you still need the open and close parentheses, e.g. student1.studentaverage().
Here's how I would do it.
class studentgrades:
def __init__(self,grades):
self.grades = grades
self.num_of_grades = len(grades)
self.grades_total = sum(grades)
def average(self):
return self.grades_total / self.num_of_grades
grades = [100, 82, 75, 93]
student1 = studentgrades(grades)
print student1.average()
This way, you only have to pass the class a list of grades.
Better yet, using TheSoundDefense's suggestion, you could eliminate the need to define two more variables in the __init__ method by calling the sum and len methods of the grades list inside the average method.
class studentgrades:
def __init__(self,grades):
self.grades = grades
def average(self):
return sum(self.grades) / len(self.grades)
grades = [100, 82, 75, 93]
student1 = studentgrades(grades)
print student1.average()
1 Comment
average() function as return sum(self.grades) / len(self.grades).The point i figured out is that Firstly the code is not properly intented ..Secondly you cant call a function like student1.studentaverage.Instead of that it must be student1.studentaverage(param).Replace the param with your parameter.
Comments
Adam Smith is correct. You need to modify the studentaverage function to add a return value:
def studentaverage(self,average):
...
return self.average
And then modify your print statement as such:
print studentaverage()
defs indented one place further? Second, you say you’re getting an error; can you edit your question to include the exact error message?num_of_gradesandgrades_total, and then immediately resetting them to 0? Also, yourstudentaveragefunction is not returning anything, it's just setting an object variable. That's fine, but you can't expectprint student1.studentaverageto work correct - especially because without(), you're not actually calling the function.