"Thinking like CS" problem I can't solve

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Pavluck

    #1

    "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)

  • Mel Wilson

    #2
    Re: "Thinki ng like CS" problem I can't solve

    Alex Pavluck wrote:[color=blue]
    > 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:
    > [ ... ][/color]

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

    Comment

    • gry@ll.mit.edu

      #3
      Re: &quot;Thinki ng like CS&quot; problem I can't solve

      Alex Pavluck wrote:[color=blue]
      > 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)[/color]

      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(ValueErr or):
      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:

      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.

      Comment

      • gry@ll.mit.edu

        #4
        Re: &quot;Thinki ng like CS&quot; problem I can't solve

        Alex Pavluck wrote:[color=blue]
        > 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)[/color]

        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(ValueErr or):
        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:

        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.

        Comment

        Working...