Passing variables between methods in Python?
These need to be instance variables:
class simpleclass(object):
def __init__(self):
self.x = None
self.y = None
def getinput (self):
self.x = input("input value for x: ")
self.y = input("input value for y: ")
def calculate (self, z):
print self.x+self.y+z
You want to use self.x
and self.y
. Like so:
class simpleclass (object):
def getinput (self):
self.x = input("input value for x: ")
self.y = input("input value for y: ")
def calculate (self, z):
print self.x+self.y+z
x
and y
are local variables. they get destroyed when you move out from the scope of that function.
class simpleclass (object):
def getinput (self):
self.x = raw_input("input value for x: ")
self.y = raw_input("input value for y: ")
def calculate (self, z):
print int(self.x)+int(self.y)+z