Python计算器错误输出

时间:2014-11-09 13:35:51

标签: python output calculator

所以我使用python 2.7.8创建了一个计算器,除非有一件事情,否则一切都会像它应该的那样工作。我想收到错误消息" ERROR:Minimum Input:Number Operator Number!"例如,当我尝试计算" 2 +"。但我无法让它发挥作用。如果有人能指出我做了什么错误,那将是惊人的。 这是不能正常工作的:

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

以下是整个代码:

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()

感谢您的回答!

2 个答案:

答案 0 :(得分:1)

如果你拥有它,代码将无法正常工作。相反,您需要将此部分更改为if语句或手动raise例外:

旧版本 - 无效。

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

新版本 - 选项A.

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

新版本 - 选项B。

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

我使用RuntimeError作为示例,您可能希望改用something more suitable

答案 1 :(得分:0)

try:
    assert len(self.input) >= 3
    # ...Your code...
except AssertionError:
    print "ERROR: Minimum Input: Number Operator Number!"

问题是“除了”必须跟一个例外,“len(self.input)&gt; = 3”也不例外! (AssertionError是一个例外)