如何在python 3中创建一个简单的计算器?

时间:2017-07-29 16:48:33

标签: python function calculator python-3.6

我正在使用python 3制作这个计算器,这是我到目前为止所做的:

print("Welcome to Calculator!")

class Calculator:
    def addition(self,x,y):
        added = x + y
        return added
    def subtraction(self,x,y):
        subtracted = x - y
        return subtracted
    def multiplication(self,x,y):
        multiplied = x * y
        return multiplied
    def division(self,x,y):
        divided = x / y
        return divided

calculator = Calculator()

print("1 \tAddition")
print("2 \tSubtraction")
print("3 \tMultiplication")
print("4 \tDivision")
operations = int(input("What operation would you like to use?:  "))

x = int(input("How many numbers would you like to use?:  "))

if operations == 1:
    a = 0
    sum = 0
    while a < x:
        number = int(input("Please enter number here:  "))
        a += 1
        sum = calculator.addition(number,sum)
    print("The answer is", sum)
if operations == 2:
    s = 0
    diff = 0
    while s < x:
        number = int(input("Please enter number here:  "))
        s += 1
        diff = calculator.subtraction(number,diff)
    print("The answer is", diff)
if operations == 3:
    m = 0
    prod = 1
    while m < x:
        number = int(input("Please enter number here:  "))
        m += 1
        prod = calculator.multiplication(number, prod)
    print("The answer is", prod)
if operations == 4:
    d = 0
    quo = 1
    while d < x:
        number = int(input("Please enter number here:  "))
        d += 1
        quo = calculator.division(number, quo)
    print("The answer is", quo)

加法和乘法工作正常,减法和除法是这里的问题。减法的一个例子是,如果我尝试使用两个数字,9和3,我会得到-6 ......这绝对是不正确的。至于除法,如果我尝试将两个数字分别为10和2,我会得到0.2,这也是错误的。对于分区我已经尝试过切换数字和现状,并且遇到同样的问题(10/2),我会得到0.05 ...而且,我不想使用python的任何内置函数,所以只是帮助我以最简单的方式解决这些错误。

2 个答案:

答案 0 :(得分:0)

你的算法对于减法和除法是错误的。让我们看一下减法:

s = 0
diff = 0
while s < x:
    number = int(input("Please enter number here:  "))
    s += 1
    diff = calculator.subtraction(number,diff)

在脑海中逐步解决这个问题。如果您使用两个数字(9和3)进行操作,则第一次迭代将获得diff = 9 - 0 = 9,下一次将获得diff = 3 - 9 = (-6)。那不会起作用。

如果所有项(但第一项)被否定,则减法为加法。如果您认为它很简单:

terms = []
for _ in range(x):  # this is a more idiomatic way to iterate `x` times
    number = int(input("Please enter number here: "))
    terms.append(number)
head, tail = terms[0], terms[1:]
result = head
for term in tail:
    result -= tail
return result

你当然可以进一步浓缩

terms = [int(input("Please enter number here: ")) for _ in range(x)]
terms[1:] = [x * (-1) for x in terms[1:]]
return sum(terms)

与分工相似,一步一步走在你的脑海中:

d = 0
quo = 1
while d < x:
    number = int(input("Please enter number here:  "))
    d += 1
    quo = calculator.division(number, quo)
print("The answer is", quo)

使用10和2,您首先获得quo = 10 / 1 = 10,然后获得quo = 2 / 10 = 0.2。就像我们可以将减法推广为尾部加法一样,我们可以将除法推广为尾倒置乘法。

24 / 2 / 3 / 4 == 24 * (1/2) * (1/3) * (1/4)

我们可以类似地编写算法。

terms = [int(input("Please enter number here: ")) for _ in range(x)]
head, tail = terms[0], terms[1:]
result = head
for term in tail:
    result /= term

请注意,所有这些内容也可以使用functools.reduce和其中一个operator函数进行编写

terms = [int(input("Number please! ")) for _ in range(int(input("How many numbers? ")))]
sum_ = functools.reduce(operator.add, terms)
diff = functools.reduce(operator.sub, terms)
prod = functools.reduce(operator.mul, terms)
diff = functools.reduce(operator.truediv, terms)

答案 1 :(得分:0)

考虑减法选项(使用您在问题中提供的测试输入): 我们说我们给2个数字。 对于第一个数字,我们给出了9. diff = number - diff = 9 - 0 = 0。 现在我们输入下一个数字3,因此diff = number - diff = 0 - 3 = -3,现在如果它正常工作,我们需要切换diff的计算,所以它应该是diff = diff - number,现在如果我们运行它会给我们-12,这在技术上是正确的答案,正如我们要求的-9-3 = -12,现在我假设你真的想找到9-3 = 6,唯一的&#34 ;简单&#34;修复此问题,将diff设置为第一个数字,在本例中为9,然后执行上面所述的操作(给出预期的9 - 3 = 6)除法功能不起作用的原因与减法相同,并且您可以使用与上面类似的逻辑来修复它。

除此之外,使用加法,减法,乘法和除法方法会破坏方法的要点,即防止多次使用相同的代码,并且必须少写,但在这种情况下,运行{{1比简单地使用calculator.addition(a, b)

更长,更不灵活

python也有一个内置的计算器函数,叫做a + b,它将接受一个字符串并将其作为python代码运行,这意味着它支持BIDMAS,几个运算符,数学函数(假设你已导入它们),等