1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ipn [44]
3 years ago
14

Using the "split" string method from the preceding lesson, complete the get_word function to return the {n}th word from a passed

sentence. For example, get_word("This is a lesson about lists", 4) should return "lesson", which is the 4th word in this sentence. Hint: remember that list indexes start at 0, not 1.
def get_word(sentence, n):
# Only proceed if n is positive
if n > 0:
words = ___
# Only proceed if n is not more than the number of words
if n <= len(words):
return(___)
return("")
print(get_word("This is a lesson about lists", 4)) # Should print: lesson
print(get_word("This is a lesson about lists", -4)) # Nothing
print(get_word("Now we are cooking!", 1)) # Should print: Now
print(get_word("Now we are cooking!", 5)) # Nothing
Computers and Technology
1 answer:
lana66690 [7]3 years ago
8 0

Answer:

def get_word(sentence, n):

# Only proceed if n is positive

   if n > 0:

       words = sentence.split()

# Only proceed if n is not more than the number of words

       if n <= len(words):

           return words[n-1]

   return (" ")

print(get_word("This is a lesson about lists", 4)) # Should print: lesson

print(get_word("This is a lesson about lists", -4)) # Nothing

print(get_word("Now we are cooking!", 1)) # Should print: Now

print(get_word("Now we are cooking!", 5)) # Nothing

Explanation:

Added parts are highlighted.

If n is greater than 0, split the given sentence using split method and set it to the words.

If n is not more than the number of words, return the (n-1)th index of the words. Since the index starts at 0, (n-1)th index corresponds to the nth word

You might be interested in
Women make up 52 percent of the voting-age population and are more likely to vote, yet
NeX [460]
<span>Gender discrimination has been a prevalent issue that prevented a century of women not to participate.

Discrimination has become widespread nowadays ranging from gender discrimination, racial discrimination, work discrimination and many others.These types of discrimination are experienced by almost all people everyday without them even knowing it. Some discrimination lies behind a sweet smile and an accommodating eyes and in order to defend yourself against this discrimination you need to load up yourself of a lot of self confidence.</span>
3 0
3 years ago
Read 2 more answers
A frameset should only be used with XHTML true or false
Naddik [55]
Framesets require a special DTD. If you're writing XHTML and want to use the strict DTD, you can't use frames/framesets.
4 0
3 years ago
Write a program in C which will open a text file named Story.txt. You can create the file using any text editor like notepad etc
ddd [48]

Answer:

See explaination

Explanation:

#include <stdio.h>

#include <stdlib.h>

int main()

{

FILE * file_object;

char file_name[100];

char ch;

int characters=0, words=0;

printf("Enter source file name: ");

scanf("%s", file_name); //asking user to enter the file name

file_object = fopen(file_name, "r"); //open file in read mode

if (file_object == NULL)

{

printf("\nUnable to open file.file not exist\n"); //check if the file is present or not

}

while ((ch = fgetc(file_object)) != EOF) //read each character till the end of the file

{

if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0') //if character is space or tab or new line or null character increment word count

words++;

else

characters++; //else increment character count this assures that there is no spaces count

}

printf("The file story.txt has the following Statistics:\n"); //finally print the final statistics

if (characters > 0)

{

printf("Words: %d\n", words+1); //for last word purpose just increment the count of words

printf("Characters (no spaces): %d\n", characters);

}

fclose(file_object); //close the file object

return 0;

}

6 0
4 years ago
It’s important to consider adjusting a user’s social network permissions (or access) because
Liula [17]
<span>You may not want to share access to social networks with other members of your team. Also you may want to adjust your users social permission for security reasons, just to be on the safe side. A stronger network means you have a lower chance of a hacker breaking through your system.</span>
5 0
3 years ago
The icons to insert pictures and clip art in the PowerPoint application are located in the _____ grouping on the Insert tab.
saul85 [17]
The icons to insert pictures and clip art are located in the images grouping on the insert tab.
3 0
4 years ago
Other questions:
  • Jim has excellent oral and written communication skills. He enjoys public speaking and wants a job in which he will interact wit
    10·2 answers
  • You can decide if a paint booth filter is hazardous waste by answering which of the following questions: A) Is the paint residue
    12·2 answers
  • Anna's computer has slowed down. How might she improve her computer's performance?
    6·1 answer
  • ________ are often organize commonly used commands into a set of tabs.
    5·1 answer
  • Nascar has inserted an image into his document but needs the image to appear on its own line Which option should he choose?
    12·2 answers
  • Multiply 2.7×10^4 by 6.3×10^6​
    13·2 answers
  • Customizing ads to people who had earlier visited the site is
    14·1 answer
  • What function key is used to enable the spelling and grammar function
    14·1 answer
  • Can anyone help me with these assignments??? Hands On Assignments- Insertion of Symbols, Special Characters, and Images.... and
    9·1 answer
  • True or false, USBs are slower than RAM.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!