Skip to content
Advertisement

Adding string after each vowel

I am currently on a project to develop a small, fun program that takes a name as an input and returns the name with the string “bi” after each vowel in the name.

I am encountering the problem that my program runs in an infinite loop when I have a name that has same the same vowel twice, for example: the name “aya”. technically it should return “abiyabi”

"""Welcome to the code of BoBi Sprache. This Sprache aka Language will 
put the letter "bi" after each vowel letter in your name"""

print("Welcome to the BoBiSprache programm")
Name = input("Please enter your name to be BoBied :D : ")
NameList = list(Name.lower())

vowels = ["a", "e", "i", "o", "u"]


def VowelCheck(NameList):
    for i in NameList:
        index = NameList.index(i)
        for j in vowels:
            if i == j and index == 0:
                NameList.insert(index + 1, "bi")

            elif i == j and (str(NameList[index - 1]) + str(NameList[index])) != "bi":
                NameList.insert(index + 1, "bi")


VowelCheck(NameList)
NewName = ""
NewName = (NewName.join(NameList)).title()
print("Your New Name is: %s" % NewName)

I thought first it is a problem with the first letter being a vowel. but I added an if statement that should solve that. I’m honestly out of answers now, and seeking help. You guys might see something I don’t see.

Advertisement

Answer

Build the Bobied name independent of the user-submitted name. Also note that strings are iterables in Python and there’s no need to convert the user-submitted name to a list.

username = input("Please enter your name to be BoBied :D : ")
vowels = ["a", "e", "i", "o", "u"]


def VowelCheck(name):
    bobified_name = ""
    for i in name:
        bobified_name += i
        if i in vowels:
            bobified_name += "bi"
    return bobified_name


print("Your New Name is: %s" % VowelCheck(username).title())

Now, if you want to avoid the loops and conditionals, or have large input: use str.translate(). First make a translation table, just a dict which maps vowels to bobified vowels. Then call translate() on the name to be bobified.

username = input("Please enter your name to be Bobied :D : ")
bobi_table = str.maketrans({
    'a': 'abi',
    'e': 'ebi',
    'i': 'ibi',
    'o': 'obi',
    'u': 'ubi'
})
print("Your new name is: %s" % username.translate(bobi_table))
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement