MD. ZARIF CHOWDHURY
MD. ZARIF CHOWDHURY

Reputation: 5

How do I call the variable in one method in another method inside the same class?

I'm very new to Python and I use Spyder to do my Python homework. I am making a code that requires using class to find out a student's average marks. Here's my code:

class Student:
    def __init__(self,name='default student'):
        if name==None:
            self.name='default student'
        elif name!=None:
            self.name=name
    def quizcalc(self,*marks):
        s=0
        for x in marks:
            s+=x
        return s/3
    def printdetail(self):
        print("Hello ",self.name)
        print("Your average quiz score is ",self.quizcalc) #something is not right in this line
s1 = Student()
s1.quizcalc(10)
print('--------------------------------')
s1.printdetail()
s2 = Student('Harry')
s2.quizcalc(10,8)
print('--------------------------------')
s2.printdetail()
s3 = Student('Hermione')
s3.quizcalc(10,9,10)
print('--------------------------------')
s3.printdetail()

and this is what the output looked like:

--------------------------------
Hello  default student
Your average quiz score is  <bound method Student.quizcalc of <__main__.Student object at 0x000001FDD65AEA48>>
--------------------------------
Hello  Harry
Your average quiz score is  <bound method Student.quizcalc of <__main__.Student object at 0x000001FDD65AE708>>
--------------------------------
Hello  Hermione
Your average quiz score is  <bound method Student.quizcalc of <__main__.Student object at 0x000001FDD65AE348>>

but my desired output is this:

--------------------------------
Hello default student
Your average quiz score is 3.3333333333333335
--------------------------------
Hello Harry
Your average quiz score is 6.0
--------------------------------
Hello Hermione
Your average quiz score is 9.666666666666666 

Upvotes: 0

Views: 52

Answers (2)

hostingutilities.com
hostingutilities.com

Reputation: 9519

You just need to add parenthesis

self.quizcalc()

Without parenthesis you're just printing the function itself instead of the result of running that function.


And for fun, let me illustrate a scenario where you might actually want to print out the function name like you were doing.

So lets say you have the class

class Student:
    def quizcalc_integer(self, *marks):
        pass # code goes here
    def quizcalc_decimal(self, *marks)
        pass # code goes here

    def printdetail(self, quiz_function):
        print("Hello ",self.name)
        print("Your average quiz score is ",quiz_function()) 

s1 = Student()

And if you're working with decimal numbers you can do

s1.printdetail(s1.quizcalc_integer)

And if they are not decimals

s1.printdetail(s1.quizcalc_decimal)

Very fictitious example, but if you are trying to debug what is going on and you want to know what function is stored inside of the variable quiz_function, you would then have a reason to do print(quiz_function) instead of print(quiz_function()).

And that is my long unnecessary answer to why it's even possible to print the name of a function in the first place.

Upvotes: 2

Hetal Thaker
Hetal Thaker

Reputation: 717

If you are allowed to change class strucure then you can do below changes :

  1. create instance level variable average.
  2. instead of returning value from quizcalc assign calcuated result to self.average.
  3. Inside printdetail() print instance variable self.average. Please check below code:
class Student:
    def __init__(self,name='default student'):
        self.average=0.0 #1st change
        if name==None:
            self.name='default student'
        elif name!=None:
            self.name=name
    def quizcalc(self,*marks):
        s=0
        for x in marks:
            s+=x
        self.average=s/3  #2nd change
    def printdetail(self):
        print("Hello ",self.name)
        print("Your average quiz score is ",self.average)#3rd change
s1 = Student()
s1.quizcalc(10)
print('--------------------------------')
s1.printdetail()
s2 = Student('Harry')
s2.quizcalc(10,8)
print('--------------------------------')
s2.printdetail()
s3 = Student('Hermione')
s3.quizcalc(10,9,10)
print('--------------------------------')
s3.printdetail()

Upvotes: 1

Related Questions