0

I was wondering how would I access the "eq" function from my "formula" class? What I have tried does not work and returns "unbound method". Thanks.

class formula(): 
    def eq(): 
        print "What is your Velocity: " 
        v = float(raw_input()) 
        print "What is your Intial Velocity: " 
        u = float(raw_input())  
        print "What is the time: " 
        t = float(raw_input()) 
        aa = v-u 
        answer = aa/t 
        print "Your Acceleration is: ", answer 

a =formula.eq() 

print a
MRG123
  • 123
  • 3
  • 12

3 Answers3

3

You need to give eq a self parameter, and create an instance of formula by calling it:

class formula(): 
    def eq(self): 
        print "What is your Velocity: " 
        v = float(raw_input()) 
        print "What is your Intial Velocity: " 
        u = float(raw_input())  
        print "What is the time: " 
        t = float(raw_input()) 
        aa = v-u 
        answer = aa/t 
        print "Your Acceleration is: ", answer 

formula().eq()

There is no point in storing the return value, you don't return anything here.

I note that you don't actually use anything in your function that requires a class. You could just as well leave the class out here and not lose anything

def eq(): 
    print "What is your Velocity: " 
    v = float(raw_input()) 
    print "What is your Intial Velocity: " 
    u = float(raw_input())  
    print "What is the time: " 
    t = float(raw_input()) 
    aa = v-u 
    answer = aa/t 
    print "Your Acceleration is: ", answer 

eq()

If you wanted to put your formula in a namespace, put it in a separate module instead; put def eq() in a file formula.py and import that as import formula. No point in making eq() a staticmethod here, really.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

make it like a static method

class formula(): 
    @staticmethod
    def eq():
        v = float(raw_input("What is your Velocity: ")) 
        u = float(raw_input("What is your Intial Velocity: "))  
        t = float(raw_input("What is the time: " )) 
        aa = v - u 
        answer = aa / t 
        print "Your Acceleration is: ", answer 
        return answer

a = formula.eq() 

print a
itdxer
  • 1,236
  • 1
  • 12
  • 40
0

try:

class formula(): 
def eq(self): 
    print "What is your Velocity: " 
    v = float(raw_input()) 
    print "What is your Intial Velocity: " 
    u = float(raw_input())  
    print "What is the time: " 
    t = float(raw_input()) 
    aa = v-u 
    answer = aa/t 
    print "Your Acceleration is: ", answer 

a = formula()
print a.eq()
hyleaus
  • 755
  • 1
  • 8
  • 21