如何允许用户在班级中输入他/她想要的参数?

时间:2019-04-27 00:06:33

标签: python class oop

我制作了一个基本的计算器类,其中具有加减乘除运算功能。我如何允许用户在使用计算器时输入他/她想要的(x, y)

class Calc:
    def addition(x, y):
        print(f'your result is{x} + {y}')

    def subtraction(x, y):
        print(f'your result is{x} - {y}')

    def multiplication(x, y):
        print(f'your result is{x} * {y}')

    def division(x, y):
        print(f'your result is{x} / {y}')

    def rounded_division(x, y):
        print(f'your result is{x} // {y}')

类似于:“您想执行什么操作?:(用户输入)” 然后“您要使用什么数字执行此操作?”

1 个答案:

答案 0 :(得分:0)

您可以在类的onPressed: () => bloc.downloadData(); 方法中输入xy的输入。实例化该类时,init方法将请求输入。

__init__()

输出和计算调用如下所示:

class Calc:
    def __init__(self):
        self.x = int(input("What is x?"))
        self.y = int(input("What is y?"))

    def addition(self):
        print('your result is {}'.format((self.x+self.y)))

    def subtraction(self):
        print('your result is {}'.format((self.x-self.y)))

    def multiplication(self):
        print('your result is {}'.format((self.x*self.y)))

    def division(self):
        print('your result is {}'.format((self.x/self.y)))

    def rounded_division(self):
        print('your result is {}'.format((self.x//self.y)))