So I have the y and e endings down, just need some help with the middle. Basically, I need to double the last letter before adding er if:
the last letter is b,d,g,l,m,n,p,r,s or t
and the second to last letter is not the same as the last letter.

def add(word):
    wordlength = len(word)-1
    if word[wordlength]=="y":
        word=word.replace('y', 'i')
        word=word +'er'
    print(word)
    elif word[wordlength] == "e":
        word=word+'r'
    elif word[wordlength] == [

Should I create a list of consonants and re.search through it?

Some help would be greatly appreciated!

Recommended Answers

All 11 Replies

If the last letter is b or d or g etc., then add "word[wordlength]" on the end, nad then add 'er'. Just use a compound if statement to test for all the letters.

endswith accepts tuple argument. What happens if there is y inside word ending with 'y'? Why you do not use slicing and join method?

Improve over this script. This should work for you.

from __future__ import print_function

words=["happy",'monty python',"hello","zombi","bone","monty"]

def word(words):
 for x in words:
    if x.endswith('y'): #get the ends
      f=[y for y in x]  #strip to list for easy slicing
      fex1=f[0:-1]      #take the end away
      fex1.append("ier") # append your wish
      print("".join(fex1)) #convert to string.
    elif x.endswith('e'):
      f=[y for y in x]
      fex2=f
      str(fex2.append("r"))   
      print("".join(fex2))
##output
happier
boner
montier

help your self ;)

I see what you did with the word "bone" -_-

:L

ha ha.... not that cruel am i?
;)

# the last letter is b,d,g,l,m,n,p,r,s or t 
if word[wordlength] in ['b','d','g','l','m','n','p','r','s','t']:

    #and the second to last letter is not the same as the last letter.
    if word[wordlength] != word[wordlength-1]:
#
# or (same thing as nested if() statements)
if (word[wordlength] in ['b','d','g','l','m','n','p','r','s','t']) and \
   (word[wordlength] != word[wordlength-1]):
#
# also only the replace can be used here (but the whole idea is incorrect)
# because a new string is created, so replace can be one or many characters
    if word[wordlength]=="y":
        word=word.replace('y', 'ier') ## *****test this with the word yummy*****
#        word=word +'er'              ## slice off the last "y" instead and add "ier"

I ment this simpler way:

from __future__ import print_function

words=["happy",'monty python',"hello","zombi","bone","monty"]

def er(words):
 for x in words:
    if x.endswith('y'): #get the ends
       # add your wish
      print("".join((x[:-1],"ier"))) ## join with two tuple
    elif x.endswith('e'):
      print("".join((x,'r')))

er(words)

and that can be extended with woooee's, but my version of his parts :) would be:

theconsonants = ('b','d','g','l','m','n','p','r','s','t')
......
        elif word.endswith(theconsonants) and not word[-2] == word[-1]:
            print(''.join((word,word[-1],'er')))
        else:
            print(''.join((word,'er')))

Well more streamlined

from __future__ import print_function
words=["happy",'monty python',"hello","zombi","bone","monty"]
print([x[:-1]+"ier" for x in words if x.endswith("y")] )
print([x+"r" for x in words if x.endswith("e")] )

Getting more interesting after i opened the show ;)

My code is like this, hope I got your language correctly:

from __future__ import print_function

words = ["happy",'monty python',"hello","zombi","bone","monty","rob","will"]
theconsonants = ('b','d','g','l','m','n','p','r','s','t')

def er(words):
    for word in words:
        if word.endswith('y'):
            yield "".join((word[:-1],"ier")) ## join with two tuple
        elif word.endswith('e'):
            yield "".join((word,'r'))
        elif word.endswith(theconsonants) and not word[-2] == word[-1]:
            yield ''.join((word,word[-1],'er'))
        else:
            yield ''.join((word,'er'))

print('\n'.join(er(words)))
commented: tonyjv.. you rocks +2

mmmmm well i like your code. ;)

nice tit bits

Thanks for all the help everyone, I figured this out :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.