Newbia alert .. working my way through Easy Python Montana Edu page, need help..

Steve Holden sholden at holdenweb.com
Thu Sep 26 21:23:33 EDT 2002


"Dennis Lee Bieber" <wlfraed at ix.netcom.com> was reckless enough to opine...
> Highwaykind fed this fish to the penguins on Friday 20 September 2002
> 06:27 am:
>
> > I'm asked ( on the website : www.honors.montana.edu\_jjc\easytut
> > \node10.html ) to write a small programm to calculate rectangle,
> > square and circle thingies( forgot the English word .. ) and I came up
> > with this code :
> >
The word, of course, is "areas".

>         <snip>
>
> > But the thing keeps telling me width is not defined, also I can;t use
> > the ELIF command .. THink I could do with some help :)
> >
I saw Alex's post, and thought all his advice very sound. Nice of you to
reply, by the way, telling him it was all working. Even the martellibot
likes to know it's appreciated :-).

>
>         Where to begin... I see way too many errors to intersperse
corrections.
>
> 1)      Python indentation controls block structure!

While true, this is not by ityself helpful. Alex's fairly exact pointer
about the indentation of the print statement, plus conjecture that this
might solve the elif problem apparently did the trick.

> 2)      You never assign values to those names it is complaining about --
> rather you are inputting to "a", "b", etc. but never passing /those/ to
> the functions.
>
>         Compare:
>
> import math     #get the long definition of PI
>
> def area_rect(width, height):
>         return width * height
>
> def area_square(side):          # squares are special rectangles
>         return area_rect(side, side)
>
> def area_circle(radius)
>         return math.pi * (radius * radius)      # r*r is likely faster
than r**2
>
Your comment might be important in the context of a program that had to
compute the area of three gazillion circles, but is definitely premature
optimization as far as the original poster is concerned.
>
> option = "prompt"       #filler for first pass
> while option != "q":
>         if option == "r":
>                 w = input("Width")
>                 h = input("Height")
>                 print "Area of Rectangle is ", area_rect(w, h)
>
>         elif option == "s":
>                 s = input("Side")
>                 print "Area of Square is ", area_square(s)
>
>         elif option == "c":
>                 r = input("Radius")
>                 print "Area of Circle is ", area_circle(r)
>
>         else:
>                 print "Options:"
>                 print " 'r'     Calculate Area of Rectangle"
>                 print " 's'     Calculate Area of Square"
>                 print " 'c'     Calculate Area of Circle"
>                 print " 'q'     Quit program"
>                 print " <other> Display this help"
>
>         option = raw_input("Option")
>
This is a nice command loop structure, although it would work just as well
if the initial statement were replaced by

    option = None

That probably only makes a difference to certain people, so feel free to
ignore it -- the existing code will certainly not work any worse...

regards
-----------------------------------------------------------------------
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Previous .sig file retired to                    www.homeforoldsigs.com
-----------------------------------------------------------------------






More information about the Python-list mailing list