How can I make a simple calculator in python 3?

巧了我就是萌 提交于 2019-12-13 09:39:46

问题


I'm making this calculator using python 3, and this is what I have so far:

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)

Addition and multiplication works just fine, subtraction and division are the problems here. One example for subtraction is if I tried using two numbers, 9 and 3, I would get -6... That is definitely incorrect. As for division, if I tried dividing two numbers, 10 and 2, I would get 0.2, which is also wrong. For division I've tried switching number and quo, and with the same problem (10 / 2), I would get 0.05... Also, I don't want to use any of the built-in functions for python, so just help me fix these errors the easiest way possible.


回答1:


Your algorithm is wrong for subtraction and division. Let's look at subtraction:

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

Step through this in your head. If you're operating on two numbers (9 and 3), you will get diff = 9 - 0 = 9 on the first iteration and diff = 3 - 9 = (-6) on the next. That won't work.

Subtraction is addition if all the terms (but the first) are negated. If you think about it like that it's fairly simple:

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

You can certainly condense this further

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

Similarly with division, step through in your head:

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)

With 10 and 2, you get first quo = 10 / 1 = 10 then quo = 2 / 10 = 0.2. Like we can generalize subtraction to be tail-negated addition, we can generalize division to be tail-inverted multiplication.

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

And we can write the algorithm similarly.

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

Note that all of these can also be written with functools.reduce and one of the operator functions

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)



回答2:


Consider the subtraction option (with the test input you supplied in the question): We say we give 2 numbers. For the first number we give 9. So diff = number - diff = 9 - 0 = 0. Now we enter the next number, 3, therefore diff = number - diff = 0 - 3 = -3, now if it were to work correctly, we need to switch around calculation of diff, so it should be diff = diff - number, and now if we run it it will give us -12, which is the technically the correct answer, as we are asking -9-3 = -12, now I'm assuming you actually want to find 9-3 = 6, the only "simple" fix to this, is to set diff as the first number, in this case 9, and then doing what I said above (giving the expected 9 - 3 = 6) The reason the division function does not work is the same as subtraction, and you can use similar logic as above to fix it.

In addition to this, making an addition, subtraction, multiplication and division method defeats the point of methods, that is to prevent using the same code several times, and having to write less, but in this case, running calculator.addition(a, b) is longer and less flexible than simply using a + b

Also python has an in-built calculator function, called eval, which will take a string and run it as python code, meaning it supports BIDMAS, several operators, mathematical functions (assuming you have imported them), etc.



来源:https://stackoverflow.com/questions/45391742/how-can-i-make-a-simple-calculator-in-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!