[Tutor] [Fwd: Re: trouble with "if"]

Adam Urbas jped.aru at gmail.com
Sat Jun 2 08:01:13 CEST 2007


Sorry about all that trouble for you Alan, after I posted that, I
reformatted my code a bit and updated it to do the defining and then
the calling, like you said.  The only problem I had, and I was
probably doing something wrong, was that I would do:

choice()
if choice in["1","circle"]:
    circle()

and it wouldn't actually go to circle().  So I had to put the if
statement somewhere else, can't remember where it was right now and
don't have time to find out, because I really need to be getting off
of here.

Thanks for the advice, and if you could, would you mind explaining the
global variable thing a little more, because I kept getting an error
message that was saying something to the effect that the global
variable was not defined.

Thanks,

Au

On 5/30/07, Alan Gauld <alan.gauld at btinternet.com> wrote:
> Hi Adam
>
> > flaw in my loop.  It is not infinite.
>
> In fact you don't have a loop, your program
> is really just a simple sequence.
>
> #Welcome screen:
> def welcome():
>     print "Welcome to the Area Calculation Program."
>     print
>
> welcome()
>
> AG:Since you only call this once there is no point in putting
> AG: it in a functuion
>
> #Shape choice:
> def shape():
>     print "Choose a Shape:"
>     print "1 Circle"
>     print "2 Square"
>     print "3 Triangle"
>     print "4 Exit"
>     print
>
> shape()
>
> AG: And you call this here and again at the end of the code.
> AG: But its not in a loop, its just two calls to the same function.
>
> #If circle:
> def circle():
>     shape=raw_input(">")
>     if shape in["1","circle"]:
>         print "1 Radius"
>         print "2 Diameter"
>         print "3 Circumference"
>         print "4 Area"
>         print
>
> circle()
>
> AG: And this gets a string fom the user and prints one of the
> AG: values but doesn't do anything with it. Functions should
> AG: normally do more than just print some values.
> AG: You never use the menu you print.
>
> #If radius:
> def radius():
>     radius=int(raw_input("Enter Radius:"))
>     diameter=(radius*2)
>     circumference=(diameter*3.14)
>     area=(radius**2*3.14)
>     print "Diameter",diameter
>     print "Circumference",circumference
>     print "Area",area
>     print
>     print
>
> def choice():
>     given=raw_input(">")
>     if given in["1","radius"]:
>         radius()
> choice()
>
> AG: Now you call radius which actually does some work
>
> shape()
> choice()
>
> AG: Then you call shape but don't use the result then call
> AG: choice which only works for circles.
>
> AG:If we use the more conventional approach of defining the
> AG: functins first then calling them at the end your code
> AG: looks like this:
>
>
> welcome()
> shape()
> circle()
> choice()  #--> which calls radius()
> shape()
> choice()
>
> As you see there is no loop just a sequence of function calls.
>
> Now if I rename your functions to reflect what theyactually do:
>
> printWelcomeMessage()
> printShapeMenu()
> getShapeAndIfCirclePrintCircleParameterNames()
> getParameterAndIfRadiuscallRadius()
> -----> getRadiusCalculateResultsAndPrintThem()
> printShapeMenu()
> getParameterAndIfRadiuscallRadius()
> -----> getRadiusCalculateResultsAndPrintThem()
>
> Hopefully this illustrates several things:
> 1) Naming your functions to reflect what they actually do
> helps see what the code does
> 2) Especially if you group the function calls together at the
> end of your program
> 3) Your functions are mixing up the presentation of menus
> and the getting of values.
>
> A better structure might be to do something like:
>
> shape = 0       # a variable to store the shape
> printWelcomeMessage()
> printShapeMenuAndGetValue()  #---store he result in the shape variable
> if shape in['1','circle']:
>     doAllCircleStuff()
> else: print "Only circles supported for now!"
>
> And then its easy to wrap that in a real loop
>
> shape = 0       # a variable to store the shape
> printWelcomeMessage()
> while shape == 0:
>    printShapeMenuAndGetValue()  #---store he result in the shape
> variable
>    if shape in['1','circle']:
>       doAllCircleStuff()
>       shape = 0     # reset shape back to zero
>    else: print "Only circles supported for now!"
>
> Can you rewrite your functions to match that? You need to change
> the shape() function so that it sets the shape global variable to the
> value input by the user. You will need to include the statement
> global shape at the top of the function for that to work. The other
> (and better) way is to use the return statement to return the value.
>
> BTW I'm not really suggesting you use those long names,
> they were just to point out what the functions were really doing!
>
> HTH,
>
> Alan G.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list