Home > Software engineering >  Issue checking input type in Pyton 2.7 vs Python 3
Issue checking input type in Pyton 2.7 vs Python 3

Time:06-24

I need to re-use a script I wrote in Python 3.10 using Python 2.7; when I change the interpreter to 2.7, script below errors with following message: TypeError: unbound method CheckInput() must be called with Elem instance as first argument (got int instance instead)

Trying to resolve this for hours but I'm a beginner and am definitely missing something. Aim of the program is to add a few values as user input and later on print them out. Code below:

class Elem():
    Elem_count = 0

    def CheckInput(input):
        try:
            # Convert it into integer
            val = int(input)
        except ValueError:
            try:
                # Convert it into float
                val = float(input)
            except ValueError:
                print("You have entered a string, please use numbers!")

    def Add_Elem(self):
        if Elem.Elem_count == 0:
            print('Enter first Elem info:')
            self.width = input('width: ')
            Elem.CheckInput(self.width)
            self.height = input('Height: ')
            Elem.CheckInput(self.height)
            self.depth = input('depth: ')
            Elem.CheckInput(self.depth)
        else:
            print('Enter second Elem info:')
            self.width = input('width: ')
            Elem.CheckInput(self.width)
            self.height = input('Height: ')
            Elem.CheckInput(self.height)
            self.depth = input('depth: ')
            Elem.CheckInput(self.depth)

        Elem.Elem_count  = 1
        if Elem.Elem_count > 2:
            exit

    def Display_Elem(self):
        print('\n')
        if Elem.Elem_count == 0:
            print('Element ')
            print('width: '   self.width)
            print('Height: '   self.height)
            print('Depth: '   self.depth)
        else:
            print('Element ')
            print('width: '   self.width)
            print('Height: '   self.height)
            print('Depth: '   self.depth)


def main():
    First_Elem = Elem()
    Second_Elem = Elem()
    First_Elem.Add_Elem()
    Second_Elem.Add_Elem()

    print ("Elements added:")
    First_Elem.Display_Elem()
    Second_Elem.Display_Elem()


if __name__ == "__main__":
    main()

CodePudding user response:

Your issue is here:

def CheckInput(input):

For a normal class method, you'll have a definition like this:

def my_method(self, some_parameter):

The self here refers to the class. While you can pass in some other class, the behavior quickly becomes undefined. Your TypeError points to this, though: CheckInput() must be called with Elem instance as first argument (got int instance instead).

This means an int was passed - which is likely the parameter you meant. All you need is this:

def CheckInput(self, candidate_input):
    try:
        val = int(candidate_input)
    except ValueError:
        try:
            val = float(candidate_input)
        except ValueError:
            print(f"Could not convert '{candidate_input}' to a number!")
    return val

Note the change of input to candidate_input to avoid builtin name collision, and the use of the candidate input in the output string to allow the user to understand what was happening with bad input.

Also, it's likely that you want to return the value that you have - though it's not great design to have a function that returns one of two types of numbers.

CodePudding user response:

To me the easiest fix would be to remove the checkinput method from your elem class ; it is useless for it to be in the class because it doesn't use the attributes of the class.

Also, you shouldn't name a variable "input" as it is the name of a builtin in both Python 2 and Python 3.

def CheckInput(data):
    try:
        # Convert it into integer
        val = int(data)
    except ValueError:
        try:
            # Convert it into float
            val = float(data)
        except ValueError:
            print("You have entered a string, please use numbers!")

class Elem():
    Elem_count = 0

    def Add_Elem(self):
        if Elem.Elem_count == 0:
            print('Enter first Elem info:')
            self.width = input('width: ')
            CheckInput(self.width)
            self.height = input('Height: ')
            CheckInput(self.height)
            self.depth = input('depth: ')
            CheckInput(self.depth)
        else:
            print('Enter second Elem info:')
            self.width = input('width: ')
            CheckInput(self.width)
            self.height = input('Height: ')
            CheckInput(self.height)
            self.depth = input('depth: ')
            CheckInput(self.depth)

        Elem.Elem_count  = 1
        if Elem.Elem_count > 2:
            exit

    def Display_Elem(self):
        print('\n')
        if Elem.Elem_count == 0:
            print('Element ')
            print('width: '   self.width)
            print('Height: '   self.height)
            print('Depth: '   self.depth)
        else:
            print('Element ')
            print('width: '   self.width)
            print('Height: '   self.height)
            print('Depth: '   self.depth)
  • Related