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'
Python please...
- 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'

Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 3 images









