Area Calculator

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.
Note: posts here are not necessarily endorsed by the community, and may represent amateur or even bad practices.

Area Calculator

This is my first Python script and it's an area calculator for plane shapes, I wrote it simply to learn. I would appreciate any feedback.

Code: Select all
`import mathdef plane_shape():    print('')    shape = input('Please enter either triangle, square, rectangle or circle to calculate its area: ')        plane_shape = ['triangle', 'square', 'rectangle', 'circle']    while shape not in plane_shape:        print('You entered:', shape)        print('')        shape = input('Please enter either triangle, square, rectangle or circle to calculate it\'s area: ')        if shape == 'triangle':        triangle()    elif shape == 'square':        square()    elif shape == 'rectangle':        rectangle()    else:        circle()          def triangle():    base = float(input('Please input the base of the triangle: '))    height = float(input('Please input the vertical height of the triangle: '))    area = (base*height)/2    print('The area of the triangle = ', area)    plane_shape()    def square():    length = float(input('Please input the length of one side of the square: '))    area = length*length    print('The area of the square = ', area)    plane_shape()    def rectangle():    width = float(input('Please input the width of the rectangle: '))    height = float(input('Please input the height of the rectangle: '))    area = width*height    print('The area of the rectangle =', area)       plane_shape()     def circle():    radius = float(input('Please input the radius of the circle: '))    area = (math.pow(radius,2))*math.pi    print('The area of the circle = ', area)    plane_shape()    plane_shape()`

-JAP
Last edited by japhomepc on Wed Mar 19, 2014 8:37 pm, edited 2 times in total.
japhomepc

Posts: 2
Joined: Mon Jan 27, 2014 3:13 pm
Location: MI

Re: Area Calculator

Try not to duplicate code
Code: Select all
`shape = input('Please enter either triangle, square, rectangle or circle to calculate it\'s area: ')`

is called before the while loop and then again inside the while loop
A loop can be started with
Code: Select all
`while True`

which will continue looping until you use break, then the input only needs to be asked once.

The loop doesnt offer a way out, you could add 'End' as a choice to exit the loop.

The loop at the moment doesn't actualy get a chance to loop, functions are calling functions inside functions, if there was a print at the end of
Code: Select all
`def plane_shape():`
it would never happen, the code creates a shape, the user enters square which calls function square, which at the end calls another plane_shape without having finished the first plane_shape yet.

Rather then calling plane_shape at the end of each shape, let the shape finish so the code returns to the original plane_shape which can then end and loop itself back around.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Area Calculator

One more thing:
japhomepc wrote:
Code: Select all
`def plane_shape():    # ...    plane_shape = ['triangle', 'square', 'rectangle', 'circle']`

so what is plane_shape ? a function or a list? Be a bit more creative with names. I would use plane_shapes for the latter, not only to distibguish it from the function name but to reflect its real sense.
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm

Re: Area Calculator

Thank you for the feedback, I made the recommended changes.

Code: Select all
`import mathdef program_loop():        while True:            print('')        shape = input('Please enter either triangle, square, rectangle or circle to calculate its area, end to exit: ')            if shape == 'end':            break            plane_shapes = ['triangle', 'square', 'rectangle', 'circle']        if shape not in plane_shapes:            print('You entered:', shape)                                 if shape == 'triangle':            triangle()        elif shape == 'square':            square()        elif shape == 'rectangle':            rectangle()        elif shape == 'circle':            circle()          def triangle():    base = float(input('Please input the base of the triangle: '))    height = float(input('Please input the vertical height of the triangle: '))    area = (base*height)/2    print('The area of the triangle = ', area)    def square():    length = float(input('Please input the length of one side of the square: '))    area = length*length    print('The area of the square = ', area)    def rectangle():    width = float(input('Please input the width of the rectangle: '))    height = float(input('Please input the height of the rectangle: '))    area = width*height    print('The area of the rectangle =', area)       def circle():    radius = float(input('Please input the radius of the circle: '))    area = (math.pow(radius,2))*math.pi    print('The area of the circle = ', area)    program_loop()`

-JAP
japhomepc

Posts: 2
Joined: Mon Jan 27, 2014 3:13 pm
Location: MI