I am trying to teach myself a little coding through the book “learning python the hard way” and am struggling with% d /% s /% r when binding to displaying a floating point number. How to pass a floating point number to a format character? At first I tried% d, but my answers displayed as integers .... I had some success with% r, but I was on the assumption that it was usually reserved for debugging? I realized that for separation in python 2.x, you need to manually float the denominator so that it works correctly for some reason.
Code example:
def divide (a, b): print "WE DIVIDING %r and %r NOW" % (a, b) return a / float(b) print "Input first number:" first = float(raw_input("> ")) print "OK, now input second number:" second = float(raw_input("> ")) ans = divide(first, second) print "DONE: %r DIVIDED BY %r EQUALS %r, SWEET MATH BRO!" % (first, second, ans)
user2134093
source share