Develop a Python program which will convert English words into their Pig Latin f
ID: 3843392 • Letter: D
Question
Develop a Python program which will convert English words into their Pig Latin form, as described below. The program will repeatedly prompt the user to enter a word. First convert the word to lower case. The word will be converted to Pig Latin using the following rules: If the word begins with a vowel, append "way" to the end of the word If the word begins with a consonant, remove all consonants from the beginning of the word and append them to the end of the word. Then, append "ay" to the end of the word. For example: "dog" becomes "ogday" "scratch" becomes "atchscray" "is" becomes "isway" "apple" becomes "appleway" "Hello" becomes "ellohay" "a" becomes "away" The program will halt when the user enters "quit" (any combination of lower and upper case letters, such as "QUIT", "Quit" or "qUIt"). Suggestions: Use .lower () to change the word to lower case. How do you find the position of the first vowel? I like using enumerate (word) as in for i, c h enumerate (word) where ch is each character in the word and i is the character's index (position) Use slicing to isolate the first letter of each word. Use slicing and concatenation to form the equivalent Pig Latin words. Use the in operator and the string "aeiou" to test for vowels. Good practice: define a constant VOWELS = 'aeiou'Explanation / Answer
Code:
#!/usr/bin/python
def main():
# declaraing the variable and initializing to their default values
word=""
vowels = 'aeiou'
v_index = -1
flag = 1
# running an infinite loop
while(flag):
# Taking word as input from user
word = raw_input("Enter a word: ")
# computing length of the word and converting word to lower case
w_len = len(word)
word = word.lower()
# if the word entered is 'quit' it will quit out from the loop and program terminates
if word == 'quit':
break
# iterating through each letter in word to find the first position of vowel
for i, ch in enumerate(word):
if ch in vowels:
v_index = i
break
# if v_index != -1 that means given word has a vowel
if v_index != -1:
# if v_index is 0 that means vowel is at first position in the word
if v_index == 0:
print word + 'way'
# if v_index is > 0 that means vowel is not at first position but somewhere in the middle or last
elif v_index > 0:
print word[v_index:w_len] + word[0:v_index] + 'ay'
if __name__=='__main__':
main()
Execution and output:
186590cb0725:Python bonkv$ python pig_latin.py
Enter a word: dog
ogday
Enter a word: scratch
atchscray
Enter a word: is
isway
Enter a word: apple
appleway
Enter a word: Hello
ellohay
Enter a word: a
away
Enter a word: quit
186590cb0725:Python bonkv$ python pig_latin.py
Enter a word: bat
atbay
Enter a word: QUIT
186590cb0725:Python bonkv$ python pig_latin.py
Enter a word: ball
allbay
Enter a word: Quit
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.