Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

266
Visualizações
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)
over 4 years ago · Santiago Trujillo
4 Respostas
Responde à pergunta

0

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())
over 4 years ago · Santiago Trujillo Relatório

0

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  
over 4 years ago · Santiago Trujillo Relatório

0

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 classmethods:

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
>>> 
over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda