19.5. Exercises

  1. Below, we have provided buggy code. Add a try/except clause so the code runs without errors. If a blog post didn’t get any likes, a ‘Likes’ key should be added to that dictionary with a value of 0.

  2. The code below assigns the 5th letter of each word in food to the new list fifth. However, the code currently produces errors. Insert a try/except clause that will allow the code to run and produce of list of the 5th letter in each word. If the word is not long enough, it should not print anything out. Note: The pass statement is a null operation; nothing will happen when it executes.

19.5.1. Contributed Exercises

Below, we have provided buggy code. Add a try/except clause so the code runs without errors.

Add exception handling to the get_value() function so that it, if an IndexError exeception occurs because the specified index does not exist, the function returns the keyword None. Do not handle any other types of exceptions.

Calculate the log base ten of each value in xvals and store the result in a list called solution. Use exception handling to skip any calculations that produce math domain errors.

The code below assigns the 5th letter of each word in food to the new list fifth. However, the code currently produces errors. Insert a try/except clause that will allow the code to run and produce of list of the 5th letter in each word. If the word is not long enough, it should not print anything out. Note: The pass statement is a null operation; nothing will happen when it executes.

You have attempted of activities on this page