I was wondering if in python there was a simple way to run code if a try statement was successful that wasn't in the try statement itself. Is that what the else or finally commands do (I didn't understand their documentation)? I know I could use code like this:
successful = False try: #code that might fail successful = True except: #error handling if code failed if successful: #code to run if try was successful that isn't part of try
but I was wondering if there was a shorter way.
You want else:
else
for i in [0, 1]: try: print '10 / %i: ' % i, 10 / i except: print 'Uh-Oh' else: print 'Yay!'
2.1m questions
2.1m answers
60 comments
57.0k users