# Solutions # pig_latin.py # use pig_latin function we wrote in homework 2 VOWELS = ['a', 'e', 'i', 'o', 'u'] DOUBLES = ['th', 'st', 'qu', 'pl', 'tr'] PUNCTUATION = ['.', '?', '!', ',', ':', ';'] def pig_latin(word): # word is a string to convert to pig-latin if not isinstance(word, str): return 'Word must be a string' # determine if there is punctuation involved punctuation = False if word[-1] in PUNCTUATION: punctuation = True punc_char = word[-1] word = word[:-1] # follow all rules to convert to pig latin if len(word) >= 2 and word[:2] in DOUBLES: pig_word = word[2:] + word[:2] + 'ay' elif word[0] in VOWELS: pig_word = word + 'hay' else: pig_word = word[1:] + word[0] + 'ay' # finally, if there was punctuation, add it at the end! if punctuation: pig_word += punc_char return pig_word def pig_latin_sentence(): # asks user for sentence of only words and spaces to convert to pig-latin while True: sentence = raw_input('Please enter a sentence with only words and spaces: ') if sentence == 'QUIT': break # prepare sentence for translation lower = sentence.lower() wordlist = lower.split() converted = '' for word in wordlist: converted += (pig_latin(word)) + ' ' # remove last space converted = converted[:-1] print converted pig_latin_sentence()