converting a nested try/except statement into try/except/else

Slawomir Nowaczyk slawomir.nowaczyk.847 at student.lu.se
Thu Aug 10 12:12:49 EDT 2006


On Wed, 09 Aug 2006 18:51:04 +0000 (GMT)
John Salerno <johnjsal at NOSPAMgmail.com> wrote:

#> try:
#>      int(text)
#> except ValueError:
#>      self.error_message()
#>      return False
#> else:
#>      return True
#> 
#> I think it's much cleaner, but obviously I lost the test in the
#> original if statement.
#> 
#> So my question is, can I still retain this second structure and
#> still test for > 0, but not have any extra nesting?

How about

try:
    if int(text) > 0:
       return True
except ValueError:
    pass
self.error_message()
return False

-- 
 Best wishes,
   Slawomir Nowaczyk
     ( Slawomir.Nowaczyk at cs.lth.se )

In 10 minutes, a hurricane releases more energy than all of the world's
nuclear weapons combined.




More information about the Python-list mailing list