I am trying to teach myself Python on code academy and have written the following basic code, which is not working as whatever the input the outcome is 'Please Enter a Valid Number' and I get a message saying "Oops, try again! Make sure area_of_circle takes exactly one input (radius)."
import math
radius = raw_input("Enter the radius of your circle")
def area_of_circle(radius):
if type(radius) == int:
return math.pi() * radius**2
elif type(radius) == float:
return math.pi() * radius**2
else:
return "'Please enter a valid number'"
print "Your Circle area is " + area_of_circle(radius) + " units squared"
The original assignment is:
Write a function called
area_of_circlethat takesradiusas input and returns the area of a circle. The area of a circle is equal to pi times the radius squared. (Use the math.pi in order to represent Pi.)
type(variablename) == sometype. At best, useisinstance(variablename, sometype). Or just don't test at all, assume it's a valid type, that's more pythonic. Catch the exception if need be, but don't test for specific types.math.piis a number, not a function, so you'll need to changemath.pi()tomath.pi.