2 Replies - 301 Views - Last Post: 22 March 2013 - 01:50 PM Rate Topic: -----

#1 pidgeonpancakes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-August 11

I can't get a floating point number to calculate in a simple progr

Posted 22 March 2013 - 01:25 PM

I'm trying to learn Python on my own and I'm doing little projects to learn the nuances of the language. In the simple script below I'm trying to take a radius of a circle as input from a user and print out the circumference of the circle. It always rounds to a whole number though and I can't figure out why. Can someone give me some hints or point me in the right direction as to why? I'm not looking for full solutions here just a little direction.

import math

print "What is the radius of your circle?(Enter as x.xx)"

radius = float(raw_input ("> "))
circumference = radius * 2.00 * math.pi

print "The circumference of your circle is: %d" % circumference


Is This A Good Question/Topic? 0
  • +

Replies To: I can't get a floating point number to calculate in a simple progr

#2 pidgeonpancakes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-August 11

Re: I can't get a floating point number to calculate in a simple progr

Posted 22 March 2013 - 01:31 PM

I just figured out a solution. I changed the formatting from %d to %s and that solved the problem.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2091
  • View blog
  • Posts: 3,185
  • Joined: 21-June 11

Re: I can't get a floating point number to calculate in a simple progr

Posted 22 March 2013 - 01:50 PM

%f (for floating point) works too. The format specifier %d is used to print integers and, as you noticed, it will convert anything that isn't an integer to an integer before printing it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1