0

This is my solution so far:

def chemificateWord(word):

vowels = ('a', 'e', 'i', 'o', 'u', 'y','A','E','I','O','U','Y')

word2 = []

for letter in word:

    if not word.endswith(vowels):
        word2 = word + 'ium'
    return word2 

''' So when I now print(chemificateWord(Californ))

The result I want is:

Californ**ium**

So I want to replace to last ending letters if they are vowels with 'ium', could anyone help? '''

Akshath Mahajan
  • 238
  • 2
  • 11
Robin Vdc
  • 11
  • 3

2 Answers2

3
def chemificateWord(word):    
    vowels = ('a', 'e', 'i', 'o', 'u', 'y','A','E','I','O','U','Y')

    word2 = word

    while word2.endswith(vowels):
        word2 = word2[:-1]
    word2 += "ium"
    return word2
Boendal
  • 2,461
  • 1
  • 22
  • 35
  • Thank you for the answer. Additional, if I would like to replace all ending vowels by 'ium'? So in this case this would be replacing 'ia' by 'ium'. (and even if there are more ending vowels than 2) – Robin Vdc Dec 28 '19 at 14:14
  • @RobinVdc see the new solution – Boendal Dec 28 '19 at 14:20
1

You might use re module for this task following way:

import re
def chemicate(word):
    return re.sub(r'[aeriouyAEIOUY]*$', 'ium', word)

Then:

print(chemicate('Californ'))  # Californium
print(chemicate('Baaa'))  # Bium

First argument of re.sub is so-called pattern, [] denotes any of characters inside (vowels in this case), * denotes 0 or more repeats, $ is zero-length assertion and denotes end of string.

Daweo
  • 21,690
  • 3
  • 9
  • 19