Kickstart 2020 with new opportunities! - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Built-in Functions Common to Most Data Structures
10. index() and ValueError
Summary

Instruction

Good job! In the previous exercise, we mentioned that index() returns the index of the first occurrence of a given element. What if there is no such element at all?

In such a case, index() will throw a ValueError. To prevent this, we can use the following code:

requests_processed = [12, 18, 15, 23, 24, 8, 0, 18, 14, 13]
try:
  requests_processed.index(2)
except:
  print('Element not found')

This time, instead of an error, we'll simply get the message "Element not found".

Exercise

Modify the solution from the previous exercise so that it returns an empty string if the input_letter is not found in the input_string.

Stuck? Here's a hint!

Use the try-except syntax shown in the exercise.