I would like to create a Python class with 3 functions.
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)
public class Main { static void myMethod() { System.out.println("Hello World!"); } } myMethod () prints a text (the action), when it is called. To call a method, write the method's name followed by two parentheses () and a semicolon; Inside main, call myMethod ():
To call a function in python simply mention the function. This will invoke the function logic and return the result. let's call the above functions. We called the functions we created and used them in different cases. Python functions are extremely helpful in different Python applications.
Here, only important point is that you would have to use class name just before :: operator. A member function will be called using a dot operator (.) on a object where it will manipulate data related to that object only as follows − Let us put above concepts to set and get the value of different class members in a class −
To call a user-defined static java function, type the function name followed by a set of parentheses and a semicolon. To call a predefined static function, simply type the class name followed by a dot and a function name.
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:
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.
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())
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With