3

I've written a small program to help me with my math homework:

import math
def saCircle():
    while True: 
        radius = float(raw_input("Enter the radius: "))
        print "\nFinding area with %d as the radius" % radius  
        x = math.pi * radius**2
        print "\nThe area of your circle is %d\n" % x 
saCircle() 

The problem is that it will accept a decimal number but it will not print out the value of the decimal number.

How can I fix this?

Idos
  • 15,053
  • 14
  • 60
  • 75
Snicklezs
  • 33
  • 4

2 Answers2

3

Use %f instead of %d (rounds your number to an integer) to print a float:

>>> radius = 4.4
>>> x = math.pi * radius**2
>>> print "\nThe area of your circle is %f\n" % x

The area of your circle is 60.821234

This question explains the difference very well.

Community
  • 1
  • 1
Idos
  • 15,053
  • 14
  • 60
  • 75
1

The format specifier for floats is %f, not %d (%d is for integers).

print "\nFinding area with %f as the radius" % radius
                           ^ 

See the Wikipedia article on printf format strings for more details.

arshajii
  • 127,459
  • 24
  • 238
  • 287