Reputation: 43
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))
Upvotes: 3
Views: 770
Reputation: 89926
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)))
Upvotes: 2
Reputation: 71560
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))
Upvotes: 1