如何使用 Python 3 在我的计算器中使用 if 语句进行减法和除法运算?
How can I get subtraction and division in my calculator using Python 3 to work using an if statement?
所以我之前问了一个问题,为什么我在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)。对不起,如果这是一个愚蠢的问题,因为在编码方面我是一个完全的初学者。
对于加法和乘法顺序无关紧要,即 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)
看到你的代码后,我为你做了这个,它是一样的,我使用你的计算器 class
您很容易忘记键入 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)))
所以我之前问了一个问题,为什么我在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)。对不起,如果这是一个愚蠢的问题,因为在编码方面我是一个完全的初学者。
对于加法和乘法顺序无关紧要,即 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)
看到你的代码后,我为你做了这个,它是一样的,我使用你的计算器 class 您很容易忘记键入 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)))