[Tutor] First program
Ray Parrish
crp at cmc.net
Sat Mar 13 03:11:25 CET 2010
Andre Engels wrote:
> On 3/12/10, yd <ydmt923 at gmail.com> wrote:
>
>> 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?
>>
>
> Well, many of your lines are user interface. Writing two lines of text
> to the user will in general cost you (at least) two lines of code.
>
>
>> I know i could have turned some of these things into classes and functions
>> but i don't know how to do that yet.
>>
>
> I definitely see use for functions (long lists of if...elif.. are
> usually better modeled using functions and a dictionary); using
> classes feels like overkill for something this simple.
>
>
>> Some critique of the algorithm and writing style or anything in general
>> would help and any pointers would be appreciated.
>>
>
> General remark: The usual number of spaces indented per level is 4,
> rather than the 2 that you use. This makes it easier to see the
> indentation level at first glance.
>
>
>> #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)
>>
>
> 1. The if can be shortened to
>
> if choice.isdigit():
>
> 2. This thing can be removed completely if you chance the if-statements below to
>
> if choice == "1"
>
> etcetera.
>
>
>> 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')
>>
>
> I think it's ugly to mix styles here - either use format or use commas, not both
>
>
>> 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))
>>
>
> This will cause the program to stop-with-error if something wrong is
> entered. I think that's quite rude. I would change this to:
> else:
> print('{0}, is not a valid choice'.format(choice))
>
Here's what I get from that, could you please explain why?
>>> print('{0}, is not a valid choice'.format(choice))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'format'
>>> print('{0}, is not a valid choice'format(choice))
File "<stdin>", line 1
print('{0}, is not a valid choice'format(choice))
^
SyntaxError: invalid syntax
>>> print('{0}, is not a valid choice',format(choice))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'format' is not defined
I tried taking out the dot, then replacing the dot with a comma, neither
worked.
Thanks, Ray Parrish
>
>> loop = input('Do you want to calculate the area of another shape? Y/N: ')
>> loop = loop.lower()
>>
>
>
>
>
--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com
More information about the Tutor
mailing list