0

I'm trying to solve the math problem in exercise 21 in Q&A, 24 + 34 / 100 - 1023. using functions:

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

the answer I got is -999 but when I do on calculator I got = -998.66

how to get the answer with decimal?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    To make division work properly in Python 2.x you need to `from __future__ import division`. – lmm Dec 08 '14 at 14:28

1 Answers1

0

You need to return float values , not integers. By doing so:

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

You will get the following output:

Solve 24 + 34 / 100 - 1023?
DIVIDE 34 / 100
ADD 24 + 0
SUBTRACT 24 - 1023
The Answer is -998.66
Srivatsan
  • 9,225
  • 13
  • 58
  • 83