[Tutor] First program

Ray Parrish crp at cmc.net
Sat Mar 13 03:04:42 CET 2010


Luke Paireepinart wrote:
>
>
> On Fri, Mar 12, 2010 at 4:03 AM, yd <ydmt923 at gmail.com 
> <mailto: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?
>     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.
>      
>
> One thing you should do is use more indentation.  Your program 
> structure is VERY hard to read with single space indentations.  
> Consider using 4-spaces.
> This is not overtly long, I would say it's reasonable for a first program.
> There are some things that you could probably group together, though.  
> Remember that code reuse is one of the core tenets of computer 
> programming!
>
> One example: area of square, parallelogram and rectangle are all the 
> same.  You could do something like this (assuming user enters strings 
> as choice (rather than ints)):
> if choice in ['rectangle', 'square', 'parallelogram']:
>     height = int(raw_input("Height: "))
>     if choice != 'square':
>         width = int(raw_input("Width: "))
>     else:
>         width = height
>     print "A %s with dimensions %sx%s has an area of %s." % (choice, 
> height, width, width*height)
>
>
>
Hello,

Isn't it a little more understandable to use a 
construct like the
following?

>>> print "The area of a " + Choice + "is " str(Width) + " x " + 
str(Height) + " equals " + str(Width * Height) + " 
square feet"

The area of a rectangle is 12 x 10 equals 120 
square feet.

I find that putting the variables on the end like 
that, when you're not
actually applying any special formatting to them 
makes it less readable
when I'm debugging my stuff, or when someone else 
is reading my code,
and trying to understand it.

Later, Ray Parrish



-- 
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