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: Sun, 27 
May 2007 15:10:08 -0400> From: [EMAIL PROTECTED]> To: [EMAIL PROTECTED]> CC: 
tutor@python.org> Subject: Re: [Tutor] trouble with "if"> > adam urbas said 
unto the world upon 05/27/2007 01:49 PM:> > Thank you for the help Brian.  I 
would like to ask you about these> > things.  Which one of the examples you 
gave would be most fool> > proof.> > <snip of all previous exchanges which are 
too badly formatted to be > readable>> > > Hi Adam and all,> > Adam was asking 
about how to use raw_input to drive a basic command > prompt menu system. I'd 
tried to explain that raw_input returns > strings, so his if tests which were 
something like:> > choice = raw_input("Enter an option)> if choice == 1:>      
do_option_1_stuff()> elif choice == 2:>      do_option_2_stuff()> > were not 
going to work, as choice will never be equal to an int.> > I'd sketched a few 
ways to deal with this, chiefly applying int() to > choice or comparing choice 
to '1', etc.> > That's more of less the gist of the above snippage and takes us 
more > or less up to the point where Adam asked his question above.> > I'm 
going to show you a few things that might be new to you, Adam. > Let's build up 
in steps.> > As a first pass, I would do the following:> > choice = 
int(raw_input("Please make your choice "))> > if choice == 1:>      # Option 1 
code here>      print "In option 1"> > elif choice == 2:>      # Option 2 code 
here>      print "In option 2"> > # Carry on if-test as needed (or until you 
get to the point> # of learning about dictionary dispatch :-)> > That will be 
fine, until your user enters something silly:> >  >>>> Please make your choice 
I like bikes!> Traceback (most recent call last):>    File 
"/home/brian/docs/jotter/python_scraps/adamcode.py", line 1, > in <module>>     
 choice = int(raw_input("Please make your choice "))> ValueError: invalid 
literal for int() with base 10: 'I like bikes!'>  >>>> > That's no good!> > So, 
we can use Python's exception handling tools to make this a bit > better.> > > 
try:>      choice = int(raw_input("Please make your choice "))> except 
ValueError:>      print "Please make a choice from the options offered."> > > 
if choice == 1:>      print "In option 1"> > elif choice == 2:>      print "In 
option 2"> > > There is still a problem, though:> >  >>> # Make sure the 
previous value assigned to choice is gone.>  >>> del(choice)>  >>>> Please make 
your choice I like Bikes> Please make a choice from the options offered.> 
Traceback (most recent call last):>    File 
"/home/brian/docs/jotter/python_scraps/adamcode.py", line 7, > in <module>>     
 if choice == 1:> NameError: name 'choice' is not defined>  >>>> > We've 
printed the reminder to the user, but then have gone on to > compare the 
non-existent choice value to 1, and that doesn't work so > well. It isn't 
enough to make sure that choice isn't insane---we need > to make sure that 
there is a choice value at all.> > So, better still:> > > while True:>      
try:>          choice = int(raw_input("Please make your choice "))>          # 
If the previous line worked, end the while loop. If it did>          # not 
work, we won't get here, so the loop will keep looping.>          break>      
except ValueError:>          print "Please make a choice from the options 
offered."> > if choice == 1:>      print "In option 1"> > elif choice == 2:>    
  print "In option 2"> > > Now we get the following:> > Please make your choice 
I like bikes!> Please make a choice from the options offered.> Please make your 
choice Please take this> Please make a choice from the options offered.> Please 
make your choice1> In option 1>  >>>> > > There is still a problem, though:> > 
Please make your choice 42>  >>>> > Our sanity check has only insisted that the 
user enter a value that > can be turned into an int; nothing as yet makes it be 
one of the ints > we are expecting.> > So, try this:> > while True:>      try:> 
         choice = int(raw_input("Please make your choice "))>          if 
choice < 1 or choice > 2: # Adjust to suit options>              raise 
ValueError>          break>      except ValueError:>          print "Please 
make a choice from the options offered."> > if choice == 1:>      print "In 
option 1"> > elif choice == 2:>      print "In option 2"> > > Please make your 
choice I like bikes!> Please make a choice from the options offered.> Please 
make your choice 42> Please make a choice from the options offered.> Please 
make your choice 2> In option 2>  >>>> > > Now, all of this should be formatted 
to be a bit prettier---and > displaying the allowable options up front is a 
good idea, too---but > the essential ideas are there.> > There might be some 
parts of this that are new to you, so ask away if > you've gotten a bit lost.> 
> And, I'm no expert, so if someone else comes along and says `No, don't > do 
it like that', odds are they might be right. (Especially if their > name is 
Alan, Danny, or Kent ;-)> > Best,> > Brian vdB> 
_________________________________________________________________
Create the ultimate e-mail address book. Import your contacts to Windows Live 
Hotmail.
www.windowslive-hotmail.com/learnmore/managemail2.html?locale=en-us&ocid=TXT_TAGLM_HMWL_reten_impcont_0507
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to