"Thinking like CS" problem I can't solve

Mel Wilson mwilson-to at sympatico.ca
Tue May 23 19:07:08 CEST 2006

Alex Pavluck wrote:
> Hello.  On page 124 of "Thinking like a Computer Scientist".  There is
> an exercise to take the following code and with the use of TRY: /
> EXCEPT: handle the error.  Can somone help me out?  Here is the code:
> [ ... ]

What error?

Python 2.4.2 (#1, Jan 23 2006, 21:24:54)
[GCC 3.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more 
 >>> def inputNumber(n):
...     if n == 17:
...         raise 'BadNumberError: ', '17 is off limits.'
...     else:
...         print n, 'is a nice number'
...     return n
 >>> inputNumber(17)
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 3, in inputNumber
BadNumberError: : 17 is off limits.

That error, I guess.  try:/except...: can catch it and 
prevent the traceback.  You could, say, print your own 
message and continue with your script.  Look up how to
use try:/except... .

         Cheers,        Mel.

More information about the Python-list mailing list