[Tutor] First program

yd ydmt923 at gmail.com
Fri Mar 12 11:03:48 CET 2010


Hi,
I am new to programming, altough i have read a few books about OOP and
O'Reily's Learning Python.
I would like some critique on my first program, is it normal for it to be
this long to do something simple?
I know i could have turned some of these things into classes and functions
but i don't know how to do that yet.
Some critique of the algorithm and writing style or anything in general
would help and any pointers would be appreciated.
Thanks.

#title Area calculator
#author Yudhishthir Singh


#welcome screen
msg = 'Welcome to the area calculator program '
print(msg)
print('-'*len(msg))
loop = 'y'
print()
while loop == 'y':
  #Choices menu
  print('Please select a shape\n')
  print('1. Rectangle')
  print('2. Square')
  print('3. Parallelogram ')
  print('4. Trapezoid ')
  print('5. Circle ')
  print('6. Ellipse')
  print('7. Traingle\n')
  print('-'*len(msg))
  choice = input('\nPlease enter your choice: ')
  if choice.isdigit() ==True:
    choice = int(choice)
  if choice ==1:
    #Rect
    height = input('please enter the height: ')
    width = input('please enter the width: ')
    height = int(height)
    width = int(width)
    areaRectangle = height*width
    print('\nThe area of a rectangle with {0} height and {1} width is
'.format(height,width),areaRectangle,'\n')
  elif choice ==2:
    #Square
    side = input('enter the height or width: ')
    side = int(side)
    areaSquare = side**2
    print('\nThe area of a square with a height or width of {0} is
'.format(side), areaSquare,'\n')
  elif choice ==3:
    #Parallelogram
    height = input('enter the height: ')
    base = input('enter the width aka base: ')
    height = int(height)
    base = int(base)
    areaParallelogram = height*base
    print('\nThe area of a parrallelogram with height {0} and width {1} is
'.format(height,base), areaParallelogram,'\n')
  elif choice ==4:
    #Trapezoid
    height = input('enter the height: ')
    base1 = input('enter the width of shorter side: ')
    base2 = input('enter the width of longer side: ')
    height = int(height)
    base1 = int(base1)
    base2 = int(base2)
    areaTrapezoid = (height/2)*(base1+base2)
    print('\nThe area of a trapezoid with height {0} ,base {1} and {2} is
'.format(height,base1,base2), areaTrapezoid, '\n')
  elif choice ==5:
    #Circle
    radius = input('radius: ')
    radius = int(radius)
    areaCircle = 3.14*(radius**2)
    print('\nThe area of a circle with radius {0} is '.format(radius),
areaCircle, '\n')
  elif choice ==6:
    #Ellipse
    radius1 = input('enter length of radius 1: ')
    radius2 = input('enter length of radius 2: ')
    radius1 = int(radius1)
    radius2 = int(radius2)
    areaEllipse = 3.14*radius1*radius2
    print('\nThe area of an ellipse with radii of length {0} and {1} is
'.format(radius1,radius2), areaEllipse, '\n')
  elif choice ==7:
    #Triangle
    base = input('enter base: ')
    height = input('enter height: ')
    base = int(base)
    height = int(height)
    areaTriangle = (1/2 *base)*height
    print('\nThe area of a triange with height {0} and base {1} is
'.format(height,base), areaTriangle, '\n')
  else:
    raise Exception('{0}, is not a valid choice'.format(choice))
  loop = input('Do you want to calculate the area of another shape? Y/N: ')
  loop = loop.lower()

#todo:
  #Improve error checking in individual modules
  #Turn the calculators into classes or functions
  #Make it so all the print statments for the results use the same function
or whatever
  #Make a function that turns input into integer and stores it in the
original input values place
  #etc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100312/e11df31c/attachment.html>


More information about the Tutor mailing list