[Tutor] [Fwd: Re: trouble with "if"]
Brian van den Broek
broek at cc.umanitoba.ca
Wed May 30 06:26:29 CEST 2007
Forwarding to the list as I'm out of time on this one for now.
Adam, it is better to reply to all so that messages are sent to the
list and not just the original sender. That way, more people can help,
more people can learn, and you don't have to wait on one person to
find the time.
Anticipating: when you are displaying a message in gmail, the top
right-hand side of the message display window has a clickable `Reply'.
Immediately beside that is a down pointing arrow. Click on that, and
you will have a menu with an option `Reply to all.' That's the reply
mechanism you want to use to reply to the list and the original sender
rather than just the sender.
-------- Original Message --------
Subject: Re: [Tutor] trouble with "if"
Date: Tue, 29 May 2007 23:07:57 -0500
From: Adam Urbas <jped.aru at gmail.com>
To: Brian van den Broek <broek at cc.umanitoba.ca>
References: <BAY103-W11DE2E62FD41449A952D68B12F0 at phx.gbl>
<465C64A4.60602 at cc.umanitoba.ca>
In the def welcome(), what do you put in the parentheses? Another
question, what code do you use for ending the program. I want the
user to be able to cancel the program from the main menu, where it
asks you to choose circle, square, etc. Or even perhaps allow the
user to go back to a previous menu, well I suppose that would be the
def thing() code. But what if they were at the part where the program
was asking them to input the radius, how would I give them the option
of returning to the list of given measurements of a circle?
On 5/29/07, Brian van den Broek <broek at cc.umanitoba.ca> wrote:
> adam urbas said unto the world upon 05/29/2007 12:39 PM:
> > The scary part is, I think I understand this. I copied your last
> > example and put it in IDLE and it doesn't like you code. Never
> > mind. I figured it out. So that is so it will notify you if your
> > choice is invalid. Nice lil tidbit of information there. I'll be
> > sure to use this. Oh and while your here, I'd like to ask about
> > loops I guess they are. I want to have the program go back to the
> > part where it asks for the user to select an option after it has
> > run one of its if statements.Like, when the user tells it,
> > "circle," then "radius," then enters the radius: here I would like
> > the program to go back and ask the user if they want to do anything
> > else, like find the area of a square, instead of the circle. Would
> > I have to tell python to print all those selections again, or would
> > there be a way to just return to the beginning?Thanks,Au> Date:
> Hi Adam,
> Again, I cut the mess, but I expect that if you use the gmail account
> you just posted about here on in, that will be the end of it.
> I'm glad that you are starting to have the warm glow of understanding :-)
> What you are asking about here is one reason why functions are so
> useful. They allow you (more or less) to give a name to a chunk of
> code, and then you can rerun that chunk at will by invoking the name.
> Given the problem you want to solve, I'd structure my code something
> like the following. Most of the details need to be filled in, but this
> is the skeletal structure.
> def welcome_message():
> # Some actions to invoke when the user starts the program
> print "Welcome to this program."
> def display_menu():
> # Insert code for showing the user the menu of options
> def circle_area():
> # insert code here to ask user for the radius, compute the area,
> # and display the result. You might well want to divide that up
> # into other functions that this one calls.
> def square_area():
> # Likewise
> # And so on, for each shape that you wish to handle
> def exit_message():
> # Some actions to invoke when the user chooses to terminate
> # the program.
> print "Thank you for using this program. Goodbye."
> def prompt_user():
> # Here is where the sort of code I showed you before would go.
> # I'd include an option, say 0, for exiting, which, when the
> # user picks it, you call exit_message()
> while True:
> choice = int(raw_input("Please make your choice "))
> if choice < 0 or choice > 2: # Adjust to suit options
> raise ValueError
> except ValueError:
> print "Please make a choice from the options offered."
> # sends the choice back to the code that called prompt_user
> # We won't get here until a good choice has been made
> return choice
> def main():
> # The main function driving your program. It might look
> # something like this:
> while True: # This will loop forever until you break out
> choice = prompt_user()
> if choice == 0:
> break # Terminate the while loop
> elif choice == 1: # Assuming 1 was the option for circle
> elif choice == 2:
> # And so on
> print "Please make another choice:" # Go back to top of loop
> if __name__ == '__main__':
> # This will run if you run the script, but not if you import it.
> This has not been tested (it is only an outline) but it does pass the
> only so reliable eyeball check :-)
> I'd suggest you try filling this sketch out to be useful, and post if
> you run into troubles.
> Brian vdB
More information about the Tutor