hello, its me again :-)

Paul Boddie paul at boddie.net
Wed May 23 10:40:41 CEST 2001


the_2nd_coming (jpetzold at twmi.rr.com) wrote:
>
> thanks to all who have helped me with this simple little program.
>
> I have finally gotten rid of all the errors, however, now I am getting a 
> value of none returned.
>
> the source is :
> ---------------------------------------------------------
>
> def evaluate(list):
>         num1 = int(list[0])
>         num2 = int(list[2])
>         operator = list[1]
>         
>         if operator == '+':
>                 answer = num1 + num2
>         elif operator == '-':
>                 answer = num1 - num2
>         elif operator == 'X':
>                 answer = num1 * num2
>         elif operator == 'x':
>                 answer = num1 * num2
>         elif operator == '*':
>                 answer = num1 * num2
>         elif operator == '/':
>                 answer = num1 / num2
>         else:
>                 print 'invalid operator'

You want to return a result from this function, I believe. Try...

          return answer

...at the end of this function. Of course, you could end up with an
obscure exception should an invalid operator be given, so in that case
I would recommend raising a meaningful exception instead of using the
above print statement - it's more Pythonic, honest! Replace the above
print statement with...

                  raise ValueError, 'invalid operator'

My choice of exception is probably a bit weak - you should really
invent your own exception to show how special such an occasion is. For
example, at the beginning of your program you could write...

  class InvalidOperatorError(Exception):
          pass

Now, you could raise this exception (InvalidOperatorError) instead of
ValueError in the above...

                  raise InvalidOperatorError, operator

Inheriting from Exception is likely to be magical enough for
everything to work as I have planned.

As a result of such exception raising activity, should an invalid
operator be given, your program will abort with the exception you
raised. However, you can trap (or catch, or handle) the exception and
then print your warning:

  # We might get an exception here, so announce that we're "trying"
  # something.
  try:
          # Call the function in question.
          answer = evaluate(list)

  # In the case of a new exception being defined, use this...
  except InvalidOperatorError:
          # The operator was invalid, answer won't be defined.
          print 'invalid operator'

  # If you didn't define a new exception, use this instead...
  except ValueError:
          # The operator was invalid, answer won't be defined.
          print 'invalid operator'

You can be more sophisticated than this, but I'm just giving an
outline of how you could improve the program a bit so that callers of
the evaluate function can handle the case of the invalid operator in
their own way. Whilst it may suit this program to print a message
about it, other programs which use the evaluate function might want to
suppress such messages and send rockets to Mars instead; such
situations can still be managed using the above framework. ;-)

Regards,

Paul



More information about the Python-list mailing list