Hide
Stay up to dateembedded code automagically updates, each snippet and article has a feed Join Siafoo Now or Learn More

Pig Latin in Python. Atom Feed 3

# 's
 1def main():
2 inputString = raw_input("Please enter the text to be translated: ")
3 print "Translated text:", translate(inputString)
4
5def findFirstVowel(given_word):
6 '''Returns the location of the first vowel in the given word.'''
7 for i in range(len(given_word)):
8 if checkVowel(given_word[i]) == True:
9 return i
10
11def translateWord(input_string):
12 '''Translates the word and returns the translation to the caller.'''
13 firstVowel = findFirstVowel(input_string)
14 if firstVowel == None:
15 translated = (input_string + "yay")
16 elif firstVowel == 0:
17 translated = (input_string + "yay")
18 elif firstVowel >= 1:
19 translated = (input_string[firstVowel:] + input_string[:firstVowel] + "ay")
20 else:
21 pass
22 return translated
23
24def translate(inputWords):
25 '''Accepts multiple words (separated by spaces) and will return the entire translated phrase.'''
26 wordList = inputWords.split()
27 translatedString = str()
28 for word in range(len(wordList)):
29 translatedString = (translatedString + translateWord(wordList[word]) + " ")
30 return translatedString
31
32def checkVowel(char):
33 '''Returns "True" if submitted charecter is a vowel.'''
34 vowels="AaEeIiOoUu"
35 for vowel_test in vowels:
36 if char == vowel_test:
37 return True
38 return False
39
40if __name__ == '__main__':
41 main()

Comments

over 7 years ago (26 Oct 2008 at 06:17 AM) by drummer42
Nothing fancy, but kinda cool.
over 7 years ago (26 Oct 2008 at 11:21 AM) by Stou S.
Nice, maybe now I can finally figure out how pig-latin works. You should set a filename though (misc options on edit form)