Python:使用类会输出错误 [英] Python: using classes outputs an error

查看:35
本文介绍了Python:使用类会输出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python创建一个非常简单的计算器.在仅使用函数之前,我已经做好了工作,但是事实证明添加类非常困难.

I'm trying to make a very simple calculator in python. I've made a working one before using only functions, but adding classes is proving to be hard.

def askuser():
    global Question, x, y

    Question = input("""Enter a word: ("Add", "Subtract", "Multiply", "Divise")""")
    x = int(input("Enter first number: "))
    y = int(input("Enter second number: "))

class calculating:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def add(self):
        return self.x + self.y

    def subtract(self):
        return self.x - self.y

    def multiplication(self):
        return self.x * self.y

    def division(self):
        return self.x / self.y

math = calculating

def calc():

    if Question == "Add":
        t = math.add
        print(t)

    elif Question == "Subtract":
        t = math.subtract
        print(t)

    elif Question == "Multiply":
        t = math.multiplication
        print(t)

    elif Question == "Division":
        t = math.division
        print(t)

def final():
    input("Press any key to exit:" )


def main():

    askuser()
    calc()
    final()

main()

代码运行良好,但是它给了我一个错误",而不是输出计算结果:

Code runs fine but it gives me an "error" instead of outputting a calculation:

   Enter a word: ("Add", "Subtract", "Multiply", "Divise")Add

   Enter first number: 5

   Enter second number: 5

   function add at 0x02E4EC90

   Press any key to exit:

为什么会这样?任何帮助都会很棒,谢谢.

Why would that be? Any help would be great, thanks.

推荐答案

您正在打印函数本身,而不是调用它的结果.试试:

You are printing the function itself, not the result of calling it. Try:

def calc():

if Question == "Add":
    t = math.add

elif Question == "Subtract":
    t = math.subtract

elif Question == "Multiply":
    t = math.multiplication

elif Question == "Division":
    t = math.division

print t()

或更干净但更高级:

class UserInputCalculator(object):

    operations = ["Add", "Subtract", "Multiply", "Divide"]

    def __init__(self):
        self.x = None
        self.y = None
        self.operation = None

    def run(self):
        self.prompt_for_operation()
        self.prompt_for_x()
        self.prompt_for_y()
        if self.operation == 'Add':
            return self.add()
        elif self.operation == 'Subtract':
            return self.subtract()
        elif self.operation == 'Multiply':
            return self.multiply()
        elif self.operation = 'Divide':
            return self.divide()
        else:
            raise ValueError('Unknown operation %s.' % operation)

    def prompt_for_operation(self):
        self.operation = input("Enter an operation: (%s)" % ', '.join(UserInputCalculator.operations))
        if self.operation not in UserInputCalculator.operations:
            raise ValueError('%s not a valid operation.' % self.operation)
        return self.operation

    def prompt_for_x(self):
        try:
            self.x = int(input("Enter first number: "))
        except:
            raise ValueError('Invalid value.')
        return self.x

    def prompt_for_y(self):
        try:
            self.y = int(input("Enter second number: "))
        except:
            raise ValueError('Invalid value.')
        return self.y

    def add(self):
        return self.x + self.y

    def subtract(self):
        return self.x - self.y

    def multiply(self):
        return self.x * self.y

    def divide(self):
        return self.x / self.y

calculator = UserInputCalculator()
print calculator.run()
input("Press any key to exit:" )

这篇关于Python:使用类会输出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆