0

This is my first attempt at creating and using a class. The error is occurring when I ask the user for input. I'm getting the following error:

n1 = Arithmetic.float_input("Enter your First number: ")
TypeError: float_input() missing 1 required positional argument: 'msg'

Here is my code.

# Define class
class Arithmetic:
    def float_input(self, msg): # Use this function for exception handling during user input
        while True:
            try:
                return float(input(msg))
            except ValueError:
                print("You must enter a number!")
            else:
            break
    def add(self, n1, n2):
        sum1 = n1 + n2
        print(n1,"+" ,n2,"=", sum1)
    def sub(self, n1, n2):
        diff = n1 - n2
        print(n1,"-",n2,"-", diff)
    def mult(self, n1, n2):
        product = n1 * n2
        print(n1,"*",n2, "=", product)
    def div(self, n1, n2):
        if n2 == 0:
            print(n1, "/",n2,"= You cannot divide by Zero")
        else:
            quotient = n1 / n2
            print(n1, "/",n2,"=", quotient)
    def allInOne(self, n1, n2):
        #Store values in dictionary (not required, just excercising dictionary skill)
        res = {"add": add(n1, n2), "sub": sub(n1, n2), "mult": mult(n1, n2), "div": div(n1, n2)}

# Declare variables. Ask user for input and use the exception handling function       
n1 = Arithmetic.float_input("Enter your First number: ")
n2 = Arithmetic.float_input("Enter your Second number: ")

What am I missing?

MrN1ce9uy
  • 39
  • 4

4 Answers4

5

If you're coming from a Java background, it's worth knowing you really usually don't need to wrap methods in classes in Python unless you need state provided by self.

Anyway, the error you're seeing is because your methods aren't marked @classmethod or @staticmethod and thus require an instance of the class and you're just calling them via the class itself (so no implicit instance or class object is passed in as the first parameter).

Thus your options are:

1 – create an instance of Arithmetic() and use it:

arith = Arithmetic()
n1 = arith.float_input("Enter your First number: ")
n2 = arith.float_input("Enter your Second number: ")

2 – mark the methods static, e.g.

@staticmethod
def float_input(prompt):  # note: no `self`

3 – mark the methods classmethods, e.g.

@classmethod
def float_input(cls, prompt):  # `cls` is `Arithmetic` (or its subclass) itself

4 – make the methods just regular functions without a class.

AKX
  • 152,115
  • 15
  • 115
  • 172
2

The problem is that you did not create an instance of Arithmetic before calling the method. Because you did not create an object, no instances will be passed to the self parameter. This causes the message "Enter your first number:" to be passed to the self parameter and the msg parameter to be empty.

To fix the problem, simply create an object using parentheses after the class name, example:

# Declare variables. Ask user for input and use the exception handling function       
n1 = Arithmetic().float_input("Enter your First number: ")
n2 = Arithmetic().float_input("Enter your Second number: ")

If you did not create an object on purpose, you can use the @classmethod class decorator to pass the class name to the self parameter.

# Define class

class Arithmetic:

    @classmethod
    def float_input(class_, msg): # Use this function for exception handling during user input

        while True:
            try:
                return float(input(msg))
            except ValueError:
                print("You must enter a number!")
            else:
                break
    # Code...

n1 = Arithmetic.float_input("Enter your First number: ")
n2 = Arithmetic.float_input("Enter your Second number: ")

There is also another decorator named @staticmethod. If you use this decorator, you can call the method without having an instance of Arithmetic and without having to define self in the method signature. Example:

class Arithmetic:

    @staticmethod
    def float_input(msg): # Use this function for exception handling during user input
        while True:
            try:
                return float(input(msg))
            except ValueError:
                print("You must enter a number!")
            else:
                break
    # Code...

n1 = Arithmetic.float_input("Enter your First number: ")
n2 = Arithmetic.float_input("Enter your Second number: ")
JeanExtreme002
  • 200
  • 3
  • 14
0

fix as:

# Declare variables. Ask user for input and use the exception handling function       
arithmatic = Arithmetic()
n1 = arithmatic.float_input("Enter your First number: ")
n2 = arithmatic.float_input("Enter your Second number: ")
DumTux
  • 668
  • 1
  • 9
  • 24
0

its better to instantiate the class first, and then use the proper method of it, like below

n1 = new Arithmetic()

n1.float_input('Enter your First number: ')
amdev
  • 6,703
  • 6
  • 42
  • 64