1

I am trying to replicate something like the random.radient() function where you can do something like x = random.radient(1, 10) where the value gets stored in the x for example.

File one:

class math:

    def add(self, nunber1, number2):
        self.nunber1 = nunber1
        self.number2 = number2
        return self.add(int(number1) + int(number2)) 

File two

    import fileone
    
    x = math.add(1, 2)
    print(x)

So what do I add to the code in order for me to do x = math.add(1, 2)?

ouflak
  • 2,458
  • 10
  • 44
  • 49
John Doe
  • 11
  • 1
  • 1
    Does this answer your question? [Static methods in Python?](https://stackoverflow.com/questions/735975/static-methods-in-python) – 001 Dec 04 '21 at 01:54

2 Answers2

1

File one should be like this

from test import *
x = math.add(1, 2)
print(x)

because otherwise, you don't actually import the class.

File two should be

class math:
    
    def add(number1, number2):
        number1 = nunber1
        number2 = number2
        return (int(number1) + int(number2)) 

Because you are giving the function the variable you do not need to give itself that will just result in an error.

As well as that you used the add function while returning, which will only give you an error because you're giving it one variable instead of two. Im not sure why you did it that way but it is unnecessary to try and use recursion while adding.

hbblue
  • 305
  • 3
  • 14
0

The simplest way to archive what you want is to create a file called math.py and create the add function directly in the file. Then import that file and use the add function the way you are doing it in your code.

#math.py:

def add(self, nunber1, number2):
    self.nunber1 = nunber1
    self.number2 = number2
    return self.add(int(number1) + int(number2))

#test.py:

import math
x = math.add(1, 2)
print(x)
ouflak
  • 2,458
  • 10
  • 44
  • 49