[Tutor] trouble with "if"
Brian van den Broek
broek at cc.umanitoba.ca
Sun May 27 21:10:08 CEST 2007
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
More information about the Tutor
mailing list