By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,400 Members | 903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,400 IT Pros & Developers. It's quick & easy.

try / except not worknig correctly

P: n/a
'
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

Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
'@'.join([..join(['fred','dixon']),..join(['gmail','com'])]) wrote:
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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #2

P: n/a
'@'.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 (mh******@skippinet.com.au)
- see 'Help/About PythonWin' for further copyright information.
example = raw_input('Give me a number!') Give me a number!42 type(example) <type 'str'>
I am also not sure why you expected a ValueError:
import string
a = 42
string.upper(a) 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: b = 'lower'
b.upper() 'LOWER'

You might also want to test

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

instead of your chain of if tests.

Best,

Brian vdB
Jul 18 '05 #3

P: n/a
'
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 ?

Jul 18 '05 #4

P: n/a
'@'.join([..join(['fred','dixon']),..join(['gmail','com'])]) wrote:
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:
def doA(): print 'doing A'
def doB(): 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?
Jul 18 '05 #5

P: n/a
On Sat, 12 Mar 2005 17:06:17 -0800,
'@'.join([..join(['fred','dixon']),..join(['gmail','com'])]) wrote:

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.
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.