Linked Questions

Popular Questions

Changing the last element of a string if it is an 'e'

Asked by At

I am creating a program that converts a normal English word into a form of pig-latin. I need to be able to determine if the string ends (last character) in an 'e', and if it does, replace it with ë.

I can't seem to be able to get it to work using my function. For example, the code should output the word 'happy" as "appyhë" by the end of this condition.

# User Input: Ask user for a word

WordToBeTranslated = input("Please enter a word in English: ")
WordToBeTranslatedLower = WordToBeTranslated.lower()

# Condition #1: Moving the First Letter to the end

elvish = WordToBeTranslatedLower[1:] + WordToBeTranslatedLower[0]

# Condition #2 + #3: Appending a Vowel / Appending 'en' to the end of a word

vowel = ['a', 'e', 'e', 'i', 'o', 'u']
import random
randomVowel = random.choice(vowel)
list = []
if len(WordToBeTranslated) > 4:
    elvish += randomVowel

    elvish = elvish + 'en'

# Condition #4: change all k's to c's

elvish = elvish.replace('k', 'c')

# Condition #5: Replace 'e' at end of the word with ë

if elvish[-1] == 'e':
    elvish = elvish[-1].replace('e', 'ë')
    elvish = elvish

Related Questions