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
  • 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...

  • 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...

  • 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

  • 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, 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

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • What is the program code to question Practice Exercise P5.9 from textbook C++ For Everyone 2nd...

    What is the program code to question Practice Exercise P5.9 from textbook C++ For Everyone 2nd Edition?: Writeafunction int count_words(string str) that returns a count of all words in the string str. Words are separated by spaces. For example, count_words("Mary had a little lamb") should return 5.

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
ADVERTISEMENT