[Tutor] how to rewrite area.py

William Stewart williamjstewart at rogers.com
Thu Feb 23 00:45:48 CET 2012


so I copied your format except I changed shape 3 to circle, Did I do the circle part right?
and would this be considered seperate functions?
 
Thanks for your help

import math
from math import pi

menu = """
Pick a shape(1-3):
   1) Square
   2) Rectangle
   3) Triangle
   4) Quit
"""
shape = int(input(menu))
while shape != 4:
   if shape == 1:
      length = float(input("Length: "))
      print( "Area of square = ", length ** 2 )
   elif shape == 2:
      length = float(input("Length: "))
      width = float(input("Width: "))
      print( "Area of rectangle = ", length * width )   

   elif shape == 3:
      area = float(input("Radius: "))
      circumference = float(input("radius: "))
      print( "Area of Circle = ", pi*radius**2 )
   shape = int(input(menu))
 
 
 
OR would this work better?
 
 
import math
from math import pi
print "your options are:"
print " "
print "1) Area(SQUARE)"
print "2) Area(RECTANGLE)"
print "3) Area(CIRCLE)"
print "4) Perimeter(SQUARE)"
print "5) Perimeter(RECTANGLE)"
print "6) Perimeter(CIRCLE)"
print "7) Exit"
while True: 
    selection = raw_input("Please select an option from the menu.: ")

def get_area_of_square():
    print "Please enter the width of a square"
    area = width**2
    return area

def get_area_of_rectangle():
    Print "please enter the width of a rectangle"
    area = width*height
    return area
def get_radius_of_circle():
    print "please enter the radius of a circle"
    radius = pi**2
    retrun radius
    
    
SQUARES:
    area = width*width = width**2
    perimeter = width+width+width+width = 4*width
RECTANGLES:
    area = width*height
    perimeter = width+height+width+height = 2*(width+height)
CIRCLES:
    area = pi*radius**2
    circumference = 2*pi*radius
    
 
 



--- On Tue, 2/21/12, Alan Gauld <alan.gauld at btinternet.com> wrote:


From: Alan Gauld <alan.gauld at btinternet.com>
Subject: Re: [Tutor] how to rewrite area.py
To: tutor at python.org
Date: Tuesday, February 21, 2012, 7:46 PM


On 21/02/12 23:51, William Stewart wrote:

> I need to rewrite area.py program so that it has separate functions for
> the perimeter and area of a square, a rectangle, and a circle (3.14 *
> radius**2).

You will find something similar in my tutorial in the topic Branching.
Look under the heading "Python multi-selection" and there is some code you should be able to include as template.

Have a go and come back with questions.

> I am horrible at math and I cannot even figure out what I need to do for
> this

Steven has done the math bit for you, so just plug it into the python code you need.

-- Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120222/9f65e790/attachment-0001.html>


More information about the Tutor mailing list