你还没有表现出你的努力,所以我不知道你到底出了什么问题。然而,在循环中放置try-except块应该可以做到:
def decade_total_count(decade, name):
"""
sums up the number of births of the given
female baby `name` in a 10-year period
Parameters:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
decade ... starting year of the decade
name ... female name
... str
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
count = 0
for i in range(decade, decade+10):
try:
yearly = females[(females['name']==name) & (females['year']==i)]['births'].item()
count = count + yearly
except ValueError as e:
print(f"Encountered error: {e}. Skipping.")
return count