3

I am not sure how to make this code print multiple inputs all plural. For example, if I type in the input "single-noun, single-noun, single-noun", it would print out as "single-noun, single-noun, plural-noun." For some reason, only the last string turns plural. How can I get it to print out "plural-noun, plural-noun, plural-noun?"

def double(noun):
    if noun.endswith("ey"):
        return noun + "s"    
    elif noun.endswith("y"):
        return noun[:-1] + "ies" 
    elif noun.endswith("ch"): 
        return noun + "es" 
    else:
        return noun + "s" 
noun = input("type in here")
print (double(noun))
martineau
  • 119,623
  • 25
  • 170
  • 301
Jack
  • 43
  • 5
  • 1
    I assume your `double` function is meant to be named `plural`. – jamesdlin Feb 25 '19 at 02:59
  • 1
    [this question](https://stackoverflow.com/questions/18902608/generating-the-plural-form-of-a-noun) has lots of good answers about making nouns plural (not answering your question). – khachik Feb 25 '19 at 03:01
  • @U9-Forward: I don't think it's a duplicate of that question. – martineau Feb 25 '19 at 03:40

2 Answers2

2

input() will return the entire line that the user entered. That is, if the user types bird, cat, dog, your plural function will receive a string "bird, cat, dog" instead of being called with separate "bird", "cat", and "dog" strings individually.

You need to tokenize your input string. A typical way to do this would be to use str.split() (and str.strip() to remove leading and trailing whitespace):

nouns = input("type in here").split(",")
for noun in nouns:
    print(plural(noun.strip()))

Or, if you want all of the results to be comma-separated and printed on one line:

nouns = input("type in here").split(",")
print(", ".join((plural(noun.strip()) for noun in nouns)))
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
1

Use str.split:

def double(nouns):
    l = []
    for noun in nouns.split(', '):
        if noun.endswith("ey"):
            l.append(noun + "s")   
        elif noun.endswith("y"):
            l.append(noun[:-1] + "ies")
        elif noun.endswith("ch"): 
            l.append(noun + "es")
        else:
            l.append(noun + "s")
    return ', '.join(l)
noun = input("type in here")
print (plural(noun))
U13-Forward
  • 69,221
  • 14
  • 89
  • 114