0

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>>
icktoofay
130k23 gold badges261 silver badges239 bronze badges
asked Aug 2, 2014 at 3:17
6
  • 6
    First, is your code actually indented like that or are the defs indented one place further? Second, you say you’re getting an error; can you edit your question to include the exact error message? Commented Aug 2, 2014 at 3:19
  • 2
    Also, why are you setting num_of_grades and grades_total, and then immediately resetting them to 0? Also, your studentaverage function is not returning anything, it's just setting an object variable. That's fine, but you can't expect print student1.studentaverage to work correct - especially because without (), you're not actually calling the function. Commented Aug 2, 2014 at 3:20
  • 2
    Also, he forgets to use one of the arguments. Instead, he initialized an empty list. I guess he is just looking for someone among us to finish his 101 homework.. Commented Aug 2, 2014 at 3:25
  • @Mai: Your last remark is a bit harsh. It’s an honest attempt, and the reasons why it does not work can be explained without a novel. Commented Aug 2, 2014 at 3:45
  • I see no reason to actually make a class for this anyway. Commented Aug 2, 2014 at 5:05

3 Answers 3

2

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()
answered Aug 2, 2014 at 4:03
Sign up to request clarification or add additional context in comments.

1 Comment

Though by keeping it as a function, you can calculate new averages if the grades are ever altered. I think the most friendly way of handling averages if the grades change is just to define the average() function as return sum(self.grades) / len(self.grades).
1

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.

answered Aug 2, 2014 at 3:26

Comments

0

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()
answered Aug 2, 2014 at 4:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.