473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

"Thinking like CS" problem I can't solve

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:
def inputNumber(n):
if n == 17:
raise 'BadNumberError: ', '17 is off limits.'
else:
print n, 'is a nice number'
return n

inputNumber(17)

May 23 '06 #1
3 1319
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
information.
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.
May 23 '06 #2
gry
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:

def inputNumber(n):
if n == 17:
raise 'BadNumberError: ', '17 is off limits.'
else:
print n, 'is a nice number'
return n

inputNumber(17)


Yikes! It's a very bad idea to use string literals as exceptions.
Use one of the classes from the 'exceptions' module, or derive
your own from one of them. E.g.:

class BadNum(ValueError):
pass
def inputNumber(n):
if n == 17:
raise BadNum('17 is off limits')
else:
print n, 'is a nice number'

try:
inputNumber(17)
except BadNum, x:
print 'Uh Oh!', x

Uh Oh! 17 is off limits
See:
http://docs.python.org/ref/try.html#try
especially the following bit:

....the clause matches the exception if the resulting object is
``compatible'' with the exception. An object is compatible with an
exception if it is either the object that identifies the exception, or
(for exceptions that are classes) it is a base class of the
exception,... Note that the object identities must match, i.e. it must
be the same object, not just an object with the same value.

Identity of string literals is a *very* slippery thing. Don't
depend on it. Anyway, python 2.5 gives a deprecation
warning if a string literal is used as an exception.

May 23 '06 #3
gry
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:

def inputNumber(n):
if n == 17:
raise 'BadNumberError: ', '17 is off limits.'
else:
print n, 'is a nice number'
return n

inputNumber(17)


Yikes! It's a very bad idea to use string literals as exceptions.
Use one of the classes from the 'exceptions' module, or derive
your own from one of them. E.g.:

class BadNum(ValueError):
pass
def inputNumber(n):
if n == 17:
raise BadNum('17 is off limits')
else:
print n, 'is a nice number'

try:
inputNumber(17)
except BadNum, x:
print 'Uh Oh!', x

Uh Oh! 17 is off limits
See:
http://docs.python.org/ref/try.html#try
especially the following bit:

....the clause matches the exception if the resulting object is
``compatible'' with the exception. An object is compatible with an
exception if it is either the object that identifies the exception, or
(for exceptions that are classes) it is a base class of the
exception,... Note that the object identities must match, i.e. it must
be the same object, not just an object with the same value.

Identity of string literals is a *very* slippery thing. Don't
depend on it. Anyway, python 2.5 gives a deprecation
warning if a string literal is used as an exception.

May 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Arun Bhalla | last post by:
I'm borrowing some code from Pavel's Command Prompt Explorer Bar installer to use in my own explorer bar's installer. Recently I've been thinking that using an assembly version like "1.0.*"...
6
by: Zhang Weiwu | last post by:
Hello. I am working with a php software project, in it (www.egroupware.org) Chinese simplified locate is "zh" while Traditional Chinese "tw". I wish to send correct language attribute in http...
5
by: Matthew Qvapul | last post by:
Hallo Did You find on the internet a version of this book in "info" format. Greetings Pikpus
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
1
by: David L | last post by:
I was looking for my stashed away copy of Thinking in CSharp by Bruce Eckel which I have downloaded long time ago. Sadly it is gone due to my PC harddisk reorganisation. And the author website does...
3
by: Rik Beacroft | last post by:
We have the following setup. A VB6 application calls a .NET COM component that acts mainly as a forwarding system calling methods on a web service. When you open up the VB6 object browser and...
2
by: phoover.eml | last post by:
Does anyone know how to get a copy of the "Annotated Solution Guide" for Bruce Eckel's "Thinking in C++" ebook? I have tried to order it from the mindview.net web page. However, I get an error...
1
by: Peter Hann | last post by:
What si the easiest way to switch from the source of a Form to its corrsponding Design and vice versa back ? Currently I always click on the file in the Solutions explorer. Is there way way...
8
by: =?Utf-8?B?UiBSZXllcw==?= | last post by:
I have a public class called Database.cs. It has public static functions and I call them from ProjectA. The code looks something like: int intConnectionSuccessful = Database.ConnectToDatabase();...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.