[Tutor] trouble with "if"
Brian van den Broek
broek at cc.umanitoba.ca
Tue May 29 19:36:36 CEST 2007
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
pass
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.
pass
def square_area():
# Likewise
pass
# 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:
try:
choice = int(raw_input("Please make your choice "))
if choice < 0 or choice > 2: # Adjust to suit options
raise ValueError
break
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:
welcome_message()
while True: # This will loop forever until you break out
display_menu()
choice = prompt_user()
if choice == 0:
exit_message()
break # Terminate the while loop
elif choice == 1: # Assuming 1 was the option for circle
circle_area()
elif choice == 2:
square_area()
# 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.
main()
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.
Best,
Brian vdB
More information about the Tutor
mailing list