-6

Hi Need a simple Python program to accept a list of 3 items. word_list = ['apple', 'berry', 'melon'] Using a function to convert singular to plural. If item ends with 'y', should replace that with 'ies'. Thanks so much

Danny74
  • 11
  • 4
  • What have you tried so far? – Kirk Strauser Apr 17 '20 at 00:49
  • 3
    Welcome to Stack Overflow. Please read [Open Letter to Students with Homework Problems](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems). You can't just dump your problem statement here and expect us to do it for you. It's also a good idea to take the [tour], read about what's on-topic in the [help/on-topic], and [ask]. – ChrisGPT was on strike Apr 17 '20 at 00:53

2 Answers2

1

You can use inflect package to produce the plural form.

In [109]: import inflect

In [110]:  p = inflect.engine()

In [111]: print([p.plural(word) for word in word_list])
['apples', 'berries', 'melons']
Rajat Mishra
  • 3,635
  • 4
  • 27
  • 41
  • 1
    I would pay good money to see the look on the teacher's face when 1) they grade the assignment, and 2) ask the student to explain the solution. – Kirk Strauser Apr 17 '20 at 01:00
0

Just make it so that it appends "s" unless the word ends with "y" or some other exception:

def plural(word):
    wordlist = []
    for char in word:
        wordlist.append(char)
    if word[len(word)-1] == "y":
        wordlist[len(word)-1] = "ies"
    else:
        wordlist.append("s")
    word = ""
    for i in wordlist:
        word+=i
    return word
print(plural("STRING"))


Miles Kent
  • 48
  • 8