【问题标题】:How can I get subtraction and division in my calculator using Python 3 to work using an if statement?如何使用 Python 3 在我的计算器中使用 if 语句进行减法和除法?
【发布时间】:2017-07-30 16:21:17
【问题描述】:

所以我之前问了一个问题,为什么我在 python 3.6.1 中制作的计算器中的减法和除法不起作用。你们中的很多人都慷慨地回应了,但我没有得到我想要的答案。我很抱歉,因为我应该更具体一些,但是有没有办法让我在 while 循环中添加某种 if 语句?这是我的代码:

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)

基本上,减法和除法的工作方式相反,如果我尝试输入 2 个数字,9 和 3 进行减法,我会得到 -6,而除法我会得到 0.33333333(1/3)。对不起,如果这是一个愚蠢的问题,因为在编码方面我是一个完整的初学者。

【问题讨论】:

  • this post 不回答你的问题吗?
  • @ChristianDean 很好,他们都是非常好的答案,但我并没有真正得到我想要的答案
  • 那么你在找什么?您需要清楚地说明您正在详细查看的答案四。否则,我们真的帮不上什么忙。
  • @ChristianDean 我得到了我想要的答案。我也确实说明了我正在寻找的答案,即在 while 循环中包含某种 if 语句。

标签: python calculator division python-3.6 subtraction


【解决方案1】:

对于加法和乘法顺序无关紧要,即 9+6=6+9 和 3*2=2*3。 但减法和除法并非如此,即 9-6 不等于 6-9。

在您的情况下,输入 9 和 6 的减法:

对于第一个输入:9,number = 9 & diff = 0 所以 diff = number - diff = 9 - 0 = 9

对于第二个输入:6,number = 6 & diff = 9 所以 diff = number - diff = 6 - 9 = -3

这不是我们的意图

对代码稍作修改

# For Subtraction
if operations == 2:
    s = 0
    diff = 0
    while s < x:
        number = int(input("Please enter number here:  "))
        s += 1
        if (s==1):
            diff=number
        else:
            diff = calculator.subtraction(diff, number)
    print("The answer is", diff)

#For Division
if operations == 4:
    d = 0
    quo = 1
    while d < x:
        number = int(input("Please enter number here:  "))
        d += 1
        if (d==1):
            quo=number
        else:
            quo = calculator.division(quo,number)
    print("The answer is", quo)

【讨论】:

    【解决方案2】:

    看到你的代码后,我为你制作了这个,它是一样的,我使用你的 calc 类 很容易忘记输入 Float

    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()
    num1 = raw_input('First Number >')
    num2 = raw_input('Second Number >')
    print("1 \tAddition")
    print("2 \tSubtraction")
    print("3 \tMultiplication")
    print("4 \tDivision")
    operations = raw_input('Select operation number>')
    
    if int(operations)== 1:
        print (calculator.addition(float(num1),float(num2)))
    if int(operations)== 2:
        print (calculator.subtraction(float(num1),float(num2)))
    if int(operations)== 3:
        print (calculator.multiplication(float(num1),float(num2)))
    if int(operations)== 4:
        print (calculator.division(float(num1),float(num2)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      • 2022-01-08
      • 2020-08-23
      相关资源
      最近更新 更多