Waseem Munir
Waseem Munir

Reputation: 60

I cannot get decimals value of height in cm,it gives only round output

i want to get answer like if i input value for height 1 feet,it should give 30.48 cm as output,i want only 2 values after decimal

while True:
            try:
                height  = float(input('\nEnter your height in feet:'))
            except ValueError:
                print("\nPlease enter only number")
            else:
                break
cm = (height*30.48)
print("\nYour height is  %d cm." % cm)

Upvotes: 0

Views: 235

Answers (1)

MariusG
MariusG

Reputation: 86

There are many ways to solve this; but as far as I know using the .format-method with your string is the recommended way in Python 3.
It allows you to cut off the value or to round the value in a correct way (for example, as asked, with 2 decimals):

while True:
            try:
                height  = float(input('\nEnter your height in feet:'))
            except ValueError:
                print("\nPlease enter only number")
            else:
                break
cm = (height*30.48)
print("\nYour height is {} cm.".format(round(cm,2)))

You could also round the result itself before:

cm = round(height*30.48, 2)
print("\nYour height is {} cm.".format(cm))

or use the decimal definition in the brackets as follows:

cm = (height*30.48)
print("\nYour height is {:.2f} cm.".format(cm))

Upvotes: 1

Related Questions