http://www.learnpython.org/page/Exception%20Handling
我无法修复代码以显示答案try/except
给定代码:
#Handle all the exceptions!
#Setup
actor = {"name": "John Cleese", "rank": "awesome"}
#Function to modify, should return the last name of the actor<br>
def get_last_name():
return actor["last_name"]
#Test code
get_last_name()
print "All exceptions caught! Good job!"<br>
print "The actor's last name is %s" % get_last_name()
但是,我能够使用以下代码获得正确的显示,但是它不使用try/except
#Handle all the exceptions!
#Setup
actor = {"name": "John Cleese", "rank": "awesome"}
x = actor.pop("name")
#Function to modify, should return the last name of the actor<br><br>
def get_last_name():
name = x.split(" ")
last_name = name[1] #zero-based index
return last_name
#Test code
get_last_name()
print "All exceptions caught! Good job!"
print "The actor's last name is %s" % get_last_name()