Catch KeyError and AssertionError : Except : Statement PYTHON TUTORIALS


PYTHON TUTORIALS » Statement » Except »

 

Catch KeyError and AssertionError


import sys, string

matrix = {"brazil":"br","france":"fr","argentina":"ar","usa":"us"}

def getcode(country):
     try:
         data = matrix[string.lower(country)]
         assert data != "br""You cannot select this country for this action!"
         return data
     except KeyError:
   print sys.exc_type, ":""%s is not in the list." % sys.exc_value
         print
     except AssertionError, b:
         print b
         print

while 1:
    country = raw_input("Enter the country name or press x to exit: ")
    if country == "x":
        break
    code = getcode(country)
    if code != None: 
        print "%s's country code is %s" (country, code)
        print



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Statement
» Except