Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call on a class using 3 functions?

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)
like image 437
Able Archer Avatar asked Oct 01 '21 05:10

Able Archer


People also ask

How do you call a method from the main class?

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 ():

How to call a function in Python?

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.

How to call a member function of a class using dot operator?

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 −

How do you call a function in a static function?

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.


3 Answers

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.

like image 68
geralt0 Avatar answered Sep 21 '22 05:09

geralt0


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())
like image 30
Tony Montana Avatar answered Sep 23 '22 05:09

Tony Montana


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  
like image 35
rkechols Avatar answered Sep 20 '22 05:09

rkechols