I would like to create a Python class with 3 functions.
- Function 1 will ask the user to input one number.
- Function 2 will ask the user to input another number.
- Function 3 will multiply function 1 * function 2 and return to the product.
Here is the code I have so far:
class Product:
def __init__(self, x, y):
self.x = x
self.y = y
def get_x(self):
x = int(input('What is the first number?: ')
def get_y(self):
y = int(input('What is the second number?: ')
def mult_XY(self):
return x * y
When I try calling on the class I get 'y' is not defined
.
I tried using the following code:
num = Product(x, y)
print(num.mult_XY)
CodePudding user response:
It seems that you forgot to use keyword self
in function definition. There are lot of mistakes in your code.
I think this is the correct version of your code:
class Product:
def __init__(self, x, y):
self.x = x
self.y = y
def get_x(self):
self.x = int(input('What is the first number?: '))
def get_y(self):
self.y = int(input('What is the second number?: '))
def mult_XY(self):
return self.x * self.y
And this is how you should check it working:
(Updated Version)
x = 10
y = 5
num = Product(x, y)
num.get_x()
num.get_y()
print(num.mult_XY())
CodePudding user response:
Here is a working solution. Compare it to your current solution and spot the differences. After the following code snippet, I will highlight concepts you need to research in order to understand this program better.
Here is a correct version of your code (note: there is potentially more than one solution):
class Product:
def __init__(self):
return
def get_x(self):
self.x = int(input('What is the first number?: '))
return self.x
def get_y(self):
self.y = int(input('What is the second number?: '))
return self.y
def mult_XY(self):
return self.x * self.y
p = Product()
x = p.get_x()
y = p.get_y()
result = p.mult_XY()
print('RESULT of {} * {} = {}'.format(x, y, result))
Is this the best answer? No. Depending on the specifics of your program, the structure of the code could be different.
You have gaps of knowledge on the following concepts:
- Objects and classes in Python
- Functions in Python
- Variables and scoping in Python
In order to understand better, you need to learn more about the fundamentals of Python. Here is a good resource to get you started: https://python-textbok.readthedocs.io/en/1.0/Introduction.html
After reading that, you will be able to not only answer this question, but also set the foundation for your programming knowledge. Don't give up and wish you the best.
CodePudding user response:
I am not sure if I understand you correctly, but you might need "x" and "y" to be the inputs within the class.
If so, use classmethod
s:
class Product:
@classmethod
def get_x(self):
self.x = int(input('What is the first number?: '))
return self
@classmethod
def get_y(self):
self.y = int(input('What is the second number?: '))
return self
@classmethod
def mult_XY(self):
return self.x * self.y
Ex:
>>> Product.get_x().get_y().mult_XY()
What is the first number?: 3
What is the second number?: 4
12
>>>
CodePudding user response:
To reference anything stored in the current object you need to use self.
like you did in the init function to originally save the values.
Example:
class Product:
def __init__(self, x, y):
self.x = x
self.y = y
def get_x(self):
self.x = int(input('What is the first number?: '))
def get_y(self):
self.y = int(input('What is the second number?: '))
def mult_XY(self):
return self.x * self.y