try / except not worknig correctly

?

'

the code below will not execute the except section when i enter a
number.
what am i missing ?

#########################################
..while 1:
.. print 'Pump Protection ? '
.. #line 133
.. try:
.. myInput = raw_input('A B C D E F G H I J K L ? ')
.. myInput = string.upper(myInput)
.. if myInput == 'A':break #
.. if myInput == 'B':break #
.. if myInput == 'C':break #
.. if myInput == 'D':break #
.. if myInput == 'E':break #
.. if myInput == 'F':break #
.. if myInput == 'G':break #
.. if myInput == 'H':break #
.. if myInput == 'I':break #
.. if myInput == 'J':break #
.. if myInput == 'K':break #
.. if myInput == 'L':break #
.. except ValueError:
.. print "Oops! That was no valid number. Try again..."
.. pass


..'s are to preserve indents.
fred
 
R

Robert Kern

'@'.join([..join(['fred' said:
the code below will not execute the except section when i enter a
number.
what am i missing ?

Something that will actually raise ValueError.

It looks like you could use the help of the Python Tutor mailing list.

http://www.python.org/mailman/listinfo/tutor
#########################################
.while 1:
. print 'Pump Protection ? '
. #line 133
. try:
. myInput = raw_input('A B C D E F G H I J K L ? ')
. myInput = string.upper(myInput)
. if myInput == 'A':break #
. if myInput == 'B':break #
. if myInput == 'C':break #
. if myInput == 'D':break #
. if myInput == 'E':break #
. if myInput == 'F':break #
. if myInput == 'G':break #
. if myInput == 'H':break #
. if myInput == 'I':break #
. if myInput == 'J':break #
. if myInput == 'K':break #
. if myInput == 'L':break #
. except ValueError:
. print "Oops! That was no valid number. Try again..."
. pass


.'s are to preserve indents.
fred

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
B

Brian van den Broek

'@'.join([..join(['fred', 'dixon']), ..join(['gmail', 'com'])]) said
unto the world upon 2005-03-12 19:20:
the code below will not execute the except section when i enter a
number.
what am i missing ?

#########################################
.while 1:
. print 'Pump Protection ? '
. #line 133
. try:
. myInput = raw_input('A B C D E F G H I J K L ? ')
. myInput = string.upper(myInput)
. if myInput == 'A':break #
. if myInput == 'B':break #
. if myInput == 'C':break #
. if myInput == 'D':break #
. if myInput == 'E':break #
. if myInput == 'F':break #
. if myInput == 'G':break #
. if myInput == 'H':break #
. if myInput == 'I':break #
. if myInput == 'J':break #
. if myInput == 'K':break #
. if myInput == 'L':break #
. except ValueError:
. print "Oops! That was no valid number. Try again..."
. pass


.'s are to preserve indents.
fred

Hi fred,

You are missing that raw_input returns a string.

PythonWin 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)]
on win32.
Portions Copyright 1994-2004 Mark Hammond ([email protected])
- see 'Help/About PythonWin' for further copyright information.
I am also not sure why you expected a ValueError:
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\PYTHON24\lib\string.py", line 235, in upper
return s.upper()
AttributeError: 'int' object has no attribute 'upper'

Additionally, string methods are often preferred to the string module:

You might also want to test

if myInput in ('A', 'B', 'C', 'etc'):
.. break

instead of your chain of if tests.

Best,

Brian vdB
 
?

'

1) the tutor list is really slow. but thanks.

2) Thanks Brain, i was missing the string bit. the code i posted (opps)
was not exactly where i was having problems, it just looked like it.

also thanks for the 'in' test, that will come in handy.
i am using chain because i need to do something different for each
twest.

is there a better way to return a tuple for each choice ?
 
P

Pete

'@'.join([..join(['fred' said:
1) the tutor list is really slow. but thanks.

2) Thanks Brain, i was missing the string bit. the code i posted (opps)
was not exactly where i was having problems, it just looked like it.

also thanks for the 'in' test, that will come in handy.
i am using chain because i need to do something different for each
twest.

is there a better way to return a tuple for each choice ?
You could try this:
print 'doing A'
print 'doing B'
>>> processFuncs = dict()
>>> processFuncs['A'] = doA
>>> processFuncs['B'] = doB
>>> while 1:
print 'Enter option:'
try:
myInput = raw_input('A or B?')
myInput = myInput.upper()

func = processFuncs[myInput]
func()
except KeyError, e:
print 'Opps wrong option', e
pass


Enter option:
A or B?A
doing A
Enter option:
A or B?B
doing B
Enter option:
A or B?C
Opps wrong option 'C'
Enter option:
A or B?
 
J

Jeremy Bowers

I'd also suggest

validInput = "ABCDEFGHIJKL" # and there are more clever ways to do this,
# but this will do

myInput = raw_input(" ".join(validInput) + "?")
if len(myInput) != 1:
# do whatever
pass
if myInput not in validInput:
raise ValueError("Input not in legal input: " + validInput)


Obviously not a drop-in replacement for your code, but ought to clean it
up a little.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,232
Messages
2,571,168
Members
47,803
Latest member
ShaunaSode

Latest Threads

Top