vowels= "aeiou"
word = raw_input("Type a word: ")
def change_word_into_piglatin():
if word[0] not in vowels:
print word[1:] + word[0] + "ay"
else:
print word[0:] + "shay"
print change_word_into_piglatin()
raw_input("Press ENTER to exit")
Python question?
Hi everyone! Im having trouble making a function to change one word of English
into pig-latin. I am able to change a word starting with 1 consonant, but not
a word with 2 or 3.
For example I can change
english = "hello",
into,
pig-latin = "ellohay".
but not,
english = "three"
into,
pig-latin = "eethray"
In pig-latin you are supposed to move every consonant(not just the first letter of the word)before the first vowel to the end of your word and add "ay".
I have this code so far for defining my function.
This code works fine for the word "hello", but doesnt work for "three". Can someone please help me?? And for some reason it keeps printing out "None" in the output and I wasnt sure why? Could someone help me with that too?
Thanks
Andrew
according to your directions, you would want something like
word = (some input); // array of characters
while ( the first letter is a consonant ) {
shift off the first letter;
push the same letter onto the back of the same word;
}
word = word + 'ay';
// end
So for THREE, going through the loop would create:
THREE - starting word
HREET - first loop
REETH - second loop
EETHR - third loop
EETHRAY - finish
word = (some input); // array of characters
while ( the first letter is a consonant ) {
shift off the first letter;
push the same letter onto the back of the same word;
}
word = word + 'ay';
// end
So for THREE, going through the loop would create:
THREE - starting word
HREET - first loop
REETH - second loop
EETHR - third loop
EETHRAY - finish
Ok I got this far and it seems to be working. Here is the code.
For some reason it says the piglatin word and it also prints "None" right
under it. Does anyone know why?
Also after I define this function how am I supposed to check every word in a sentence?
Thanks
Andrew
def change_word_into_piglatin(): vowels = "aeiou" word = raw_input("Type a word: ") pigword = "" count = count = 0 while word[count] not in vowels: pigword = pigword + word[count] count = count + 1 print print word[count:] + pigword + "ay"print change_word_into_piglatin()raw_input("Press ENTER to exit")
For some reason it says the piglatin word and it also prints "None" right
under it. Does anyone know why?
Also after I define this function how am I supposed to check every word in a sentence?
Thanks
Andrew
Quote:Original post by guzumba
For some reason it says the piglatin word and it also prints "None" right
under it. Does anyone know why?
Too many print statements. Look up return.
Quote:Also after I define this function how am I supposed to check every word in a sentence?
You need to change the function to take a single word as a parameter, instead of prompting the user. That way you can prompt the user for the entire sentence (or retrieve it from file, etc) and then call the function for each word in the sentence.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement