python制作简单软件,我如何在python 3中制作一个简单的计算器?

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值