Sign in to follow this  

Python question?

This topic is 3588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.
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")



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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Ok I got this far and it seems to be working. Here is the code.


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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this