我正在努力解决Q& A 24 + 34 / 100 - 1023
练习21中的数学问题。
使用功能:
def add(a, b):
print "ADD %d + %d" % (a, b)
return a + b
def subtract(a, b):
print "SUBTRACT %d - %d" % (a, b)
return a - b
def divide(a, b):
print "DIVIDE %d / %d" % (a, b)
return a / b
print "Solve 24 + 34 / 100 - 1023?"
what = subtract(add(24, divide(34,100)),1023)
print "The Answer is", what
我得到的答案是-999 但是当我在计算器上做的时候我得到了= -998.66
如何获得带小数的答案?
答案 0 :(得分:0)
您需要返回float
个值,而不是integers
。通过这样做:
def add(a, b):
print "ADD %d + %d" % (a, b)
return float(a + b) # WE RETURN A FLOAT
def subtract(a, b):
print "SUBTRACT %d - %d" % (a, b)
return float(a - b) # WE RETURN A FLOAT
def divide(a, b):
print "DIVIDE %d / %d" % (a, b)
return float(a / b) # WE RETURN A FLOAT
print "Solve 24 + 34 / 100 - 1023?"
what = subtract(add(24, divide(34.0,100)),1023)
print "The Answer is", what
您将获得以下输出:
Solve 24 + 34 / 100 - 1023?
DIVIDE 34 / 100
ADD 24 + 0
SUBTRACT 24 - 1023
The Answer is -998.66