2
\$\begingroup\$

I have to create a modularized program that can find out the diameter, circumference, and area of a circle's radius. I'm sure many of you can notice I kind of winged this from a example given from my teacher. Instead of people pointing out the fact that I coded this wrong, could you please give me reasons for what I should do so I can better understand this concept?

import math

def main():
    Radius = 0
    Diameter = 0
    Circumference = 0
    Area = 0


    Radius = GetRadius(Radius)
    Diameter = SetDiameter(Radius, Diameter)
    Circumference = SetCircumference(Radius, Circumference)
    Area = SetArea(Radius, Area)
    ShowResults(Radius, Diameter, Circumference, Area)


def GetRadius(myradius):
    myradius = float(str(input("Enter your radius: ")))
    return myradius

def SetDiameter(myradius, mydiameter):
    mydiameter = myradius * 2
    return mydiameter

def SetCircumference(myradius, mycircumference):
    PIE = 3.14159
    mycircumference = 2 * PIE * myradius
    return mycircumference

def SetArea(myradius, myarea):
    PIE = 3.14159
    myarea = PIE * myradius * myradius
    return myarea

def ShowResults(Radius, Diameter, Circumference, Area):
    print("The Diameter is",mydiameter)
    print("The Circumference is",mycircumference)
    print("The Area is",myarea)

main()
\$\endgroup\$
2
  • \$\begingroup\$ Have you learned about Classes? This would be a great example in using one. \$\endgroup\$
    – TerryA
    Commented Apr 17, 2013 at 12:02
  • \$\begingroup\$ Those "setters" make no sense: you pass a variable, set it inside the function and return it? that's not gonna work. \$\endgroup\$
    – tokland
    Commented Apr 19, 2013 at 7:51

2 Answers 2

6
\$\begingroup\$

Many little comments here to make things simpler,shorter,more pythonic :

  • PEP 0008 gives some coding guidelines for python. Among other things, it gives a naming convention. Also, you don't need the "my" prefix everywhere.

  • You can use math.pi instead of repeating our own definition of pi everywhere. (Isn't it the reason why your import math in the first place)

  • Most of your function just need to return a value and updating the parameter probably shouldn't be their responsability.

  • Most of the variables are not really required.

  • there's no point to define a main function in your case.

After taking these comments into accounts, your code looks like this :

import math

def get_radius_from_user():
    return float(input("Enter your radius: "))

def get_diameter(radius):
    return radius * 2

def get_circumference(radius):
    return 2 * math.pi * radius

def get_area(radius):
    return math.pi * radius * radius

def show_results(radius):
    print("The Diameter is",get_diameter(radius))
    print("The Circumference is",get_circumference(radius))
    print("The Area is",get_area(radius))

show_results(get_radius_from_user())
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Thanks for your review! In get_radius_from_user(), is there any reason you're converting a string to a string before converting it to a float? Also, you could write radius ** 2 instead of radius * radius but that's subjective. \$\endgroup\$
    – Adam
    Commented Apr 17, 2013 at 13:27
  • \$\begingroup\$ For the conversion, I just reused the original code without thinking too much about it as I'm not too sure to understand what was intended. Your comment about radius**2 is valid too. \$\endgroup\$
    – SylvainD
    Commented Apr 17, 2013 at 15:09
  • 1
    \$\begingroup\$ float(str(input(...)))float(raw_input(...)) \$\endgroup\$ Commented Apr 17, 2013 at 16:42
  • \$\begingroup\$ I've edited my answer. Thanks guys for the comments. \$\endgroup\$
    – SylvainD
    Commented Apr 17, 2013 at 16:54
  • \$\begingroup\$ A comma should be followed by a space, according to PEP8. \$\endgroup\$
    – Ray
    Commented Jun 7, 2014 at 5:33
1
\$\begingroup\$

Once you've learned about classes you could create a Circle object which when initiated performs all possible conversions, so that its properties - diameter, radius, area and circumference - can all be accessed without having to write anything else.

The conversion functions can be stored as a dictionary of lambdas.

import math
class Circle:
    conversions = {
        ('diameter', 'radius'): lambda d: d / 2.0,
        ('radius', 'area'): lambda r: math.pi * r ** 2,
        ('radius', 'diameter'): lambda r: r * 2.0,
        ('radius', 'circumference'): lambda r: math.pi * r * 2,
        ('area', 'radius'): lambda a: (a / math.pi) ** .5,
        ('circumference', 'radius'): lambda c: c / (math.pi * 2)
    }

    def _set(self, property, value):
        """ Set a property and recursively make all possible 
            conversions based on that property """    
        setattr(self, property, value)
        for (fro, to), conversion in self.conversions.items():
            if fro == property and getattr(self, to, None) is None:
                self._set(to, conversion(getattr(self, fro)))

    def __init__(self, **kwargs):
        self._set(*kwargs.items()[0])

my_circle = Circle(radius = 5)
print my_circle.area
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.