Implement a function printIndex() that takes a list as a parameter, prompts the user to enter a whole number n, and prints the element in position Index[n]. If the list is empty ([]) or n is not a valid index into the list, the function will not print anything. Be careful to do the correct thing with negative indices. You should assume that the user will enter a whole number when prompted, and the function will crash if the user does not enter an integer. The function should not change the list passed as a parameter. Hint: Just because this involves a list does not mean that you need a loop to solve the problem. Think carefully about the right construct to use here. The following shows the function template, and several examples runs of the function (you must show all the examples with these values in your submission): Template def printIndex(lst):     replace with your docstring       newList = lst # ________________________     elementNum = ________________________ # ________________________     if ___________________________________ # ________________________         ______________ # ________________________     elif _________________________________ # ________________________         __________________________________ # ________________________     elif _________________________________ # ________________________         __________________________________ # ________________________ (Note: these code blocks may contain more or less lines of code depending on your implementation) How it should function   >>> printIndex(['zero', 'one', 'two', 'three', 'four']) >>> Enter a whole number: 0 zero >>> printIndex(['zero', 'one', 'two', 'three', 'four']) >>> Enter a whole number: 2 two >>> printIndex(['zero', 'one', 'two', 'three', 'four']) >>> Enter a whole number: 3 three >>> printIndex(['zero', 'one', 'two', 'three', 'four']) >>> Enter a whole number: 4 four >>> printIndex(['zero', 'one', 'two', 'three', 'four']) >>> Enter a whole number: 5 >>> printIndex(['zero', 'one', 'two', 'three', 'four']) >>> Enter a whole number: -6 >>> printIndex(['zero', 'one', 'two', 'three', 'four']) >>> Enter a whole number: -5 zero >>> printIndex(['zero', 'one', 'two', 'three', 'four']) >>> Enter a whole number: -4 one >>> printIndex(['zero', 'one', 'two', 'three', 'four']) >>> Enter a whole number: -3 two >>> printIndex(['zero', 'one', 'two', 'three', 'four']) >>> Enter a whole number: -2 three printIndex(['zero', 'one', 'two', 'three', 'four']) >>> Enter a whole number: -1 four >>> printIndex(['zero', 'one', 'two', 'three', 'four']) >>> Enter a whole number: six Traceback (most recent call last):   File "", line 1, in     printIndex(['zero', 'one', 'two', 'three', 'four'])   File "C:/Users/od489/AppData/Local/Programs/Python/Python310/printIndex.py", line 5, in printIndex     elementNum = int(input('Enter a whole number: ')) ValueError: invalid literal for int() with base 10: 'six'

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Python please...

 

  1. Implement a function printIndex() that takes a list as a parameter, prompts the user to enter a whole number n, and prints the element in position Index[n]. If the list is empty ([]) or n is not a valid index into the list, the function will not print anything. Be careful to do the correct thing with negative indices. You should assume that the user will enter a whole number when prompted, and the function will crash if the user does not enter an integer. The function should not change the list passed as a parameter. Hint: Just because this involves a list does not mean that you need a loop to solve the problem. Think carefully about the right construct to use here. The following shows the function template, and several examples runs of the function (you must show all the examples with these values in your submission):

Template

def printIndex(lst):

    replace with your docstring

 

    newList = lst # ________________________

    elementNum = ________________________ # ________________________

    if ___________________________________ # ________________________

        ______________ # ________________________

    elif _________________________________ # ________________________

        __________________________________ # ________________________

    elif _________________________________ # ________________________

        __________________________________ # ________________________

(Note: these code blocks may contain more or less lines of code depending on your implementation)

How it should function

 

>>> printIndex(['zero', 'one', 'two', 'three', 'four'])

>>> Enter a whole number: 0

zero

>>> printIndex(['zero', 'one', 'two', 'three', 'four'])

>>> Enter a whole number: 2

two

>>> printIndex(['zero', 'one', 'two', 'three', 'four'])

>>> Enter a whole number: 3

three

>>> printIndex(['zero', 'one', 'two', 'three', 'four'])

>>> Enter a whole number: 4

four

>>> printIndex(['zero', 'one', 'two', 'three', 'four'])

>>> Enter a whole number: 5

>>> printIndex(['zero', 'one', 'two', 'three', 'four'])

>>> Enter a whole number: -6

>>> printIndex(['zero', 'one', 'two', 'three', 'four'])

>>> Enter a whole number: -5

zero

>>> printIndex(['zero', 'one', 'two', 'three', 'four'])

>>> Enter a whole number: -4

one

>>> printIndex(['zero', 'one', 'two', 'three', 'four'])

>>> Enter a whole number: -3

two

>>> printIndex(['zero', 'one', 'two', 'three', 'four'])

>>> Enter a whole number: -2

three

printIndex(['zero', 'one', 'two', 'three', 'four'])

>>> Enter a whole number: -1

four

>>> printIndex(['zero', 'one', 'two', 'three', 'four'])

>>> Enter a whole number: six

Traceback (most recent call last):

  File "<pyshell#11>", line 1, in <module>

    printIndex(['zero', 'one', 'two', 'three', 'four'])

  File "C:/Users/od489/AppData/Local/Programs/Python/Python310/printIndex.py", line 5, in printIndex

    elementNum = int(input('Enter a whole number: '))

ValueError: invalid literal for int() with base 10: 'six'

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education