Answer:
def get_word(sentence, n):
# Only execute if n is a positive integer
if n > 0:
words = sentence.split()
# Ensure n does not exceed total words
if n <= len(words):
return words[n-1]
return (" ")
print(get_word("This is a lesson about lists", 4)) # Expected output: lesson
print(get_word("This is a lesson about lists", -4)) # No output
print(get_word("Now we are cooking!", 1)) # Expected output: Now
print(get_word("Now we are cooking!", 5)) # No output
Explanation:
Highlights show added sections.
If n is greater than zero, the sentence is split into words using the split method.
If n is within the limit of the total number of words, the (n-1)th index will be returned, as indexing begins at 0, making (n-1) the correct reference for the nth word.