Dhanu P.

asked • 11/30/22

10.9 LAB: Exceptions with lists

Given a list of 10 names, complete the program that outputs the name specified by the list index entered by the user. Use a try block to output the name and an except block to catch any IndexError. Output the message from the exception object if an IndexError is caught. Output the first element in the list if the invalid index is negative or the last element if the invalid index is positive.

Note: Python allows using a negative index to access a list, as long as the magnitude of the index is smaller than the size of the list.

Ex: If the input of the program is:

5

the program outputs:

Name: Jane



1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.