[Tutor] First program
Alan Gauld
alan.gauld at btinternet.com
Fri Mar 12 23:04:11 CET 2010
"yd" <ydmt923 at gmail.com> wrote
> I would like some critique on my first program, is it normal for it to be
> this long to do something simple?
Its OK for a first program. There are many things that could be
improved but its not too bad.
As to length, it could be shortened a bit but this is not really a long
program.
Commercial applications like Microsoft Office contain millions of lines of
code and have teams of a hundred or more programmers working on them.
Remember that computers can only move data around and perform very
basic calculations and comparisons. All the "intelligence" comes from
you, the programmer.
The good news is that (a)you get used to it and (b) you can produce useful
programs with only a few dozen or hundreds of lines. A single programmer
can easily manage say 10,000 lines of code and thats enough to write some
seriously useful stuff.
> I know i could have turned some of these things into classes and
> functions
> but i don't know how to do that yet.
Functions would help with the structure but not really shortened it much
because you don't reuse the code all that much - although there are ways
to change that too at the expense of making the algorithms a little less
obvious..
> 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))
Consider usiong pythons long (triple quoted) strings for
this kind of thing. Its more readable IMHO and less typing
for you.
> choice = input('\nPlease enter your choice: ')
> if choice.isdigit() ==True:
> choice = int(choice)
Consider wrapping the whjole thing in a try/except structure.
Then you can convert to int() as you read the input.
Alternatively, since its only a menu and you don;t use
the ints for calculation purposes just compare to
the string values.
> if choice ==1:
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list