I started taking a "introduction to programming" class a month ago, and the introductory language we are using is Python. Because I have never programmed before and only have a month's worth of experience with Python, please forgive my mistakes.
So basically, here's the prerequisites to the "Piglatin Translator" assignment we were given:
1.) If a word has no letters, do not translate it.
2.) Punctuation and capitalization should be preserved.
3.) Separate each word into two parts. The first part is called the “prefix” and extends from the beginning of the word up to, but not including, the first vowel. (The letter “y” will be considered a vowel.) The Rest of the word is called the “stem.”
4.) The Pig Latin text is formed by reversing the order of the prefix and stem and adding the letters “ay” to the end. For example, “sandwich” is composed of “s” + “andwich” + “ay” + “.” and would translate to “andwichsay.”
5.) If the word contains no consonents, let the prefix be empty and the word be the stem. The word ending should be “yay” instead of merely “ay.” For example, “I” would be “Iyay.”
And finally, here's my code:
t = 0
again = "y"
consonants = "bBcCdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ"
while "y" in again:
word = raw_input('\nPlease type a word to translate: ')
while len(word) == 0:
print "\nYou cannot translate a non-existant word!\n"
word = raw_input('Please type a word to translate: ')
for x in word:
if (x == "a") or (x == "A") or (x == "e") or (x == "E") or (x == "i") or (x == "I") or (x == "o") or (x == "O") or (x == "u") or (x == "U") or (x == "y") or (x == "Y"):
break
t = t+1
prefix = word[:t]
stem = word[t:]
if consonants not in word:
print "Translated word:",
print word + "yay"
else:
print "Translated word:",
print stem + prefix + "ay"
again = raw_input("\nWould you like to translate another word into Piglatin? ")
I choose to splice words instead of split because it made more sense to me.
The program works correctly when the "if consonants not in word" statement is taken out,
but then that also makes #5 in the prerequisites not fulfilled.
When it is left in every translated word is out of order, and it basically just messes the process up.
Any ideas why?
Thanks in advance!