Tom Eldar
Tom Eldar

Reputation: 1

Can't get a decimal when multiplying a number by an integer from input

My goal is to fold a paper a certain number of times. For some reason it won't give me the thickness in decimals, but only as a whole number.

folds=input ("Choose how many times to fold the paper: ")

width=0.005

nWidth=width*folds

print ("The thickness after %d folds will be %d cm") %(folds, nWidth)

When I enter 200 it will print 1, but any number under 200 will print a 0. I've tried using float on nWidth and the input, but that doesn't seem to work. Can anyone help?

Edit: solved thanks to Ashwini. Changed the second %d to %f to get the float number.

Upvotes: 0

Views: 99

Answers (1)

Speaking Code
Speaking Code

Reputation: 141

I recommend to use new .format() method fo templating. It's more easy and powerful. You don't have to worry about types with it (truly duck typing).

print ("The thickness after {} folds will be {} cm".format(folds, nWidth))

More info: Format examples

Upvotes: 1

Related Questions