Question

Create a function called countWords(string): That will count words in given string by user. Use Python...

Create a function called countWords(string):

That will count words in given string by user.

Use Python 3.6

If user inputs "Mary had a little lamb" it should return 5

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#Defing the countWord function

def countWord(stri):

    count=1

    #Iterating over string

    for letter in stri:

        #Counting the white spaces for number of words

        if letter == ' ':

            count = count+1

    return count

#Asking the user to input sentence

stri= input("Enter your sentence: ")

#caliing the countWord function to count the number of words

count= countWord(stri)

#printing the number of words in a sentence

print ("\nWord Count in the sentence: "+str(count))

Output:

Mary had a little lamb Run it (F8 Save it] Hide input Absolute running time: 0.18 sec, cputime: 0.14 sec, mema Enter your sentence: Word Count in the sentence: 5

Add a comment
Know the answer?
Add Answer to:
Create a function called countWords(string): That will count words in given string by user. Use Python...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Create a Python Class Paragraph(), which can be initialized with a string that the user...

    1. Create a Python Class Paragraph(), which can be initialized with a string that the user enters (or from whatever source we so choose). So, we can create a Paragraph by: userProse1 = Paragraph(input()) 2. Write a Python method (choose a good name!) to return the count the number of words in a Paragraph object. So, an input of Paragraph ("Mary had a little lamb, the little lamb belongs to Mary") will return 11. 3. Write a Python Method to...

  • Write a function called countWords that inputs several lines of text and uses strtok to count the...

    Write a function called countWords that inputs several lines of text and uses strtok to count the total number of words. Assume that the words are separated by either spaces or newline characters. int countWords(char *string) { }

  • write a program in java that asks the user for a sentence, and then returns that...

    write a program in java that asks the user for a sentence, and then returns that string with the words backwards. For example: Enter a sentence! Mary had a little lamb. Your sentence backwards: lamb. little a had Mary Write a method called reverse() that accepts a sentence as a string, and then returns that string with the words backwards. Write a main() method that gets the string from the user and then displays the string backwards. demonstrate program by...

  • Write a C++ program that will count the number of words and vowels in a sentence....

    Write a C++ program that will count the number of words and vowels in a sentence. Create a bool function called isVowel that accepts a character as a parameter and returns a true if it’s a vowel (aeiou) and false otherwise. Create an int function named countVowels that will accept a string variable as a parameter and will return the number of vowels in it. Call the isVowel function as part of this process. Write an int function named countWords...

  • Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions...

    Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions you created. Only use one value of r and h for all the function. Ask the user for the r and h in the main() as an input, and h for all the functions. You should put the functions for areas in a separate file. ​ For example, firstDigtec7ze Write a function e digits n) (returning the number of digits in the argument returning...

  • Write a Python function, called counting, that takes two arguments (a string and an integer), and...

    Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...

  • Using Ubuntu OS... help is greatly needed! Thank you. Create an alias called "home" that does...

    Using Ubuntu OS... help is greatly needed! Thank you. Create an alias called "home" that does two things... returns you to your home directory, then does a long listing of all files found there Create a file called "Output.txt" that contains the output of the "ls /etc" command Using "cat", create a file called "poem.txt" where you enter at least one line of text ("Mary had a little lamb" will do...) Using "cat", pipe the contents of poem.txt into the...

  • In Python Create a function called sum_string ( csv_string ) Where csv_string is a string of...

    In Python Create a function called sum_string ( csv_string ) Where csv_string is a string of comma - separated values. integer that is the sum of the numerical values in the string It returns an Example: sum_string("11,22,33") Return 66

  • In Python, write a function numLetters() that keeps prompting the user for words until they hit...

    In Python, write a function numLetters() that keeps prompting the user for words until they hit return (using while loops). The function should then return the percent of 3-letter words that were entered. So, if a user entered ten words and 4 of the words had 3 letters, then the percent of 3-letter words is 40

  • I am using Python 3.5.2 2. Create a function called is_interesting that, given a string, returns...

    I am using Python 3.5.2 2. Create a function called is_interesting that, given a string, returns the Boolean value True if the number of vowels (a e i o u) in the string is a prime number (assume that the strings are always passed as lower-case letters to simplify). The function header should be as follows: def is_interesting(a_string) For example is_interesting(“yien”) returns True, is_interesting(“wang”) returns False

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT