Python Calculator Error output
Question: So I made a Calculator using python 2.7.8 and everything works like it should,
unless one thing. I want to receive the error message "ERROR: Minimum Input:
Number Operator Number!" when i for example try to calculate "2 +". But I
can't get it working. Would be amazing if someone could point out what
mistake(s) I made. Here is what is not working correctly:

    
    
    except len(self.input) < 3:
            print "ERROR: Minimum Input: Number Operator Number!"
    

And here is the whole code:

    
    
    import sys
    
    class Calculator(object):
    
        def __init__(self, input):
            self.input = input
    
        def divide(self, number1, number2):
            return number1 / number2
    
        def add(self, number1, number2):
            return number1 + number2
    
        def sub(self, number1, number2):
            return number1 - number2
    
        def mult(self, number1, number2):
            return number1 * number2
    
        def modulo(self, number1, number2):
            return number1 % number2
    
        def exponentiate(self, number1, number2):
            return number1 ** number2
    
        def print_error(self, error):
            print error
            sys.exit(-1)
    
        def print_result(self, result):
            print result
            sys.exit(0)
    
        def process_input(self):
            try:
                result = float(self.input[1])
                current_operator = ""
                for element in self.input[2:]:
                    if element in ["/", "+", "-", "*", "%", "**"]:
                        current_operator = element
                    else:
                        number2 = float(element)
                        if current_operator == "/":
                            result = self.divide(result, number2)
                        elif current_operator == "+":
                            result = self.add(result, number2)
                        elif current_operator == "-":
                            result = self.sub(result, number2)
                        elif current_operator == "*":
                            result = self.mult(result, number2)
                        elif current_operator == "%":
                            result = self.modulo(result, number2)
                        elif current_operator == "**":
                            result = self.exponentiate(result, number2)
                        else:
                            self.print_error(error)
    
                self.print_result(result)
            except ZeroDivisionError:
                print "ERROR: Caught division by zero!"
            except ValueError:
                print "ERROR: Input number could not be parsed!"
            except OverflowError:
                print "ERROR: Result too large! Overflow encountered."
            except len(self.input) < 3:
                print "ERROR: Minimum Input: Number Operator Number!"
    
    calc = Calculator(sys.argv)
    calc.process_input()
    

Thanks for any answer!


Answer: As you have it, the code won't work. Instead, you'll need to either change
this part into an `if` statement or manually `raise` an exception:

### Old version - won't work.

    
    
    try:
        # ...code...
    except len(self.input) < 3:
        print "ERROR: Minimum Input: Number Operator Number!"
    

### New version - Option A.

    
    
    if len(self.input) < 3:
        print "ERROR: Minimum Input: Number Operator Number!"
    else:
        # ...code...
    

### New version - Option B.

    
    
    try:
        if len(self.input) < 3:
            raise RuntimeError("Not enough arguments.")
        # ...code...
    except RuntimeError:
        print "ERROR: Minimum Input: Number Operator Number!"
    

I've used `RuntimeError` as an example, you might want to use [something more
suitable](https://docs.python.org/2/library/exceptions.html) instead.

