Search code examples
pythonstringpython-3.xinputfractions

How to count the number of plural words given a list of words in order to find the fraction


I'm trying to complete this homework problem:

Problem #1: "Write a program that asks the user for a list of nouns (separated by spaces) and approximates the fraction that are plural by counting the fraction that end in "s". Your program should output the total number of words and the fraction that end in "s". You should assume that words are separated by spaces (and ignore the possibility of tabs and punctuation between words).

  1. First, count the number of words in the string the user entered (Hint: count the number of spaces). Print out the number of words. Make sure this works before going onto the next part.
  2. Next, ignoring the last word (which is a special case and can be dealt with separately), count the number of words ending in 's' (Hint: count the number of "s "). Test that this part works before going on to the next step.
  3. Last, check the last word to see if it ends in "s"-- since it's the last word, the "s" will always occur at the same index in the string.*

Problem #2: If we count the number of S's, that'll count all S's in the word, not just the last one. How can I go about figuring out if the last letter in each given word ends with an S. I have this so far:

noun = input("Enter nouns: ")
print("You entered: ", noun)
words = noun.split()
print(words)
amount = len(words)
print(amount)

I don't think I could simply do a words.count('s'). Any help would be greatly appreciated, thanks.


Solution

  • Following @Billthelizard hint it looks like the easiest solution is this case is:

    plurals = noun.count('s ')