[Tutor] Error Checking/Defensive Programming

Michael Lewis mjolewis at gmail.com
Thu Jan 26 04:36:51 CET 2012


Hi everyone,

I am new to python and have a noob question.

Is it generally better to use try/except/else statements or if/elif/else?
Or, is there a time and place for each?

For a simple example, assume I want a user to enter a number

1) try:
       number = float(input('enter a number: ')
   except ValueError:
       print('enter a number.')
   else:
       print('you entered a number')

*OR*
*
*
2) number = float(input('enter a number: ')
    if number >=0 or < 0:
        print('you entered a number')
    else:
         print('enter a number.')

Thanks for the help.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120125/27179990/attachment.html>


More information about the Tutor mailing list