我如何在python 3中制作一个简单的计算器? [英] How can I make a simple calculator in python 3?

查看:333
本文介绍了我如何在python 3中制作一个简单的计算器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  print(Welcome())欢迎使用python 3计算器,这是我目前使用的计算器。计算器!)

class计算器:
def addition(self,x,y):
added = x + y
return added
def减法(self,x,y):
减去= x - y
减法运算
def乘法运算(self,x,y):
multiplied = x * y
返回乘以
def division(self,x,y):
divide = x / y
返回值除以

calculator = Calculator()

print(1 \tAddition)
print(2 \ tSubtraction)
print(3 \tMultiplication)
print(4 \ tDivision )
operations = int(input(你想用什么操作?:))

x = int(input(你想用多少个数?:) )

如果操作== 1:
a = 0
sum = 0
while a< x:
数字= int(输入(请在此输入数字:))
a + = 1
sum = calculator.addition(number,sum)
print(The答案是,总和)
如果操作== 2:
s = 0
diff = 0
while s < x:
number = int(input(请输入数字:))
s + = 1
diff = calculator.subtraction(number,diff)
print(The答案是,差异)
如果操作== 3:
m = 0
prod = 1
while m < x:
number = int(输入(请在此输入数字:))
m + = 1
prod = calculator.multiplication(number,prod)
print(The答案是,prod)
如果操作== 4:
d = 0
quo = 1
while d < x:
number = int(input(请输入数字:))
d + = 1
quo = calculator.division(number,quo)
print(The答案是,quo)

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

解决方案

考虑减法选项(使用您在问题中提供的测试输入) :
我们说我们给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 ,唯一的简单解决方法是将diff设置为第一个数字,在本例中为9,然后按照上面所述预计9 - 3 = 6)除法功能不起作用的原因与减法相同,您可以使用类似的逻辑来修复它。



在除此之外,使加法,减法,乘法和除法方法失败了方法的重点,即防止多次使用相同的代码,并且不得不少写,但在这种情况下,运行计算器.addition(a,b)很长而不是简单地使用 a + b



另外python有一个内置的计算器函数,叫做 eval ,它将采用一个字符串并将其作为python代码运行,这意味着它支持BIDMAS,几个运算符,数学函数(假设你已经导入了它们)等等。


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.

解决方案

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.

这篇关于我如何在python 3中制作一个简单的计算器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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