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.

Area Calculator

Postby japhomepc » Mon Jan 27, 2014 4:11 pm

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 math


def 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

Postby Yoriz » Mon Jan 27, 2014 7:11 pm

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.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 838
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Area Calculator

Postby ochichinyezaboombwa » Tue Jan 28, 2014 10:27 pm

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: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Area Calculator

Postby japhomepc » Thu Jan 30, 2014 6:47 pm

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

Code: Select all
import math


def 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


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 2 guests