Linh Huynh
Linh Huynh

Reputation: 25

Get decimal value when convert inches to meter, pound to kilogram

I want to get the results like this: 100 pounds = 45.3592 50 inches = 1.27 meters How do I do it? This is my code:

name = 'X'
age = 25 
height = 50 
weight = 100 
eyes = 'Black'
teeth = 'White'
hair = 'Black'
meters = 0.0254 * float(50)
kilograms = 0.453592 * float(100)

print "Let's talk about %s." % name
print "She's %d inches tall." % height
print "She's %d pounds heavy." % weight
print "Actually that's too skinny."
print "She's got %s eyes and %s hair." % (eyes, hair)
print "Her teeth are usually %s depending on the coffee." % teeth

print "If I add %d, %d and %d I get %d." % (age, height, weight, age + height + weight)
print "An other way of saying, I am %d meters and %d kilograms." % (meters, kilograms)


Upvotes: 1

Views: 166

Answers (1)

idjaw
idjaw

Reputation: 26600

Change your %d to %f:

print "An other way of saying, I am %f meters and %f kilograms." % (meters, kilograms)

If you are looking to limit to 2 decimal places, then you can set %.2f:

print "An other way of saying, I am %.2f meters and %.2f kilograms." % (meters, kilograms)

Or, you can use format:

print "An other way of saying, I am {} meters and {} kilograms.".format(meters, kilograms)

Limiting to 2 decimal places using format:

print "An other way of saying, I am {:.2f} meters and {:.2f} kilograms.".format(meters, kilograms)

Take a look at the string formatting operators

Upvotes: 3

Related Questions