我正在爲一門編程課程做一個基本的計算器,我已經閱讀了PDF的,但我不知道如何製作一個函數,然後用它來打印添加兩個數字的結果。有人可以幫我嗎?Python初學者,一個簡單的計算器程序的數學函數
def addition(intFirstOperand, intSecondOperand):
addition = intFirstOperand + intSecondOperand
print ('What mathematical operation would you like to perform? Enter a number:')
print ('1 - addition')
print ('2 - subtraction')
print ('3 - multiplication')
print ('4 - division')
intOperation = input()
intOperation = int(intOperation)
addition = '1'
subtraction = '2'
multiplication = '3'
division = '4'
if intOperation == 1 :
print ('Please enter the first operand for addition:')
intFirstOperand = input()
print ('Please enter the second operand for addition:')
intSecondOperand = input()
print addition(intFirstOperand, intSecondOperand)
if intOperation == 2 :
print ('Please enter the first operand for subtractiom:')
intFirstOperand = input()
print ('Please enter the second operand for subtraction:')
intSecondOperand = input()
if intOperation == 3 :
print ('Please enter the first operand for multiplication:')
intFirstOperand = input()
print ('Please enter the second operand for multiplication:')
intSecondOperand = input()
if intOperation == 4 :
print ('Please enter the first operand for division:')
intFirstOperand = input()
print ('Please enter the second operand for division:')
intSecondOperand = input()
在另外的功能添加一個'return'聲明: '返回addition',如果你對py3.x然後不要忘記字符串先轉換成整數('輸入()'返回py3x中的字符串)。 – 2013-02-14 22:57:38