[英]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,數字= 9&diff = 0,所以diff =數字-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)
在看到您的代碼后,我為您准備了相同的代碼,並且使用了您的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)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.