Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Solution to Codecademy (http://www.codecademy.com) Python Pig Latin Assignment.
# Convert a normal word to pig latin
# A Codecademy Python assignment (that drove many nuts!)
# Dr. Steven B. Combs, coding novice
pyg = 'ay' # create a variable that contains 'ay'
original = raw_input('Enter a word: ') # request input from the user
if len(original) > 0 and original.isalpha(): # does the variable contain any, and if so, only alpha characters
word = original.lower() # convert variable original to all lower case characters
first = word[0] # save the first character of the variable word to the new variable first
if word[0] == ("a" or "e" or "i" or "o" or "u"): # nested if statement to verify that first character in varialble word is a vowel
new_word = word + pyg # If first character is vowel, create variable new_word and append the variable pyg (ay)
print new_word # print variable new_word
else:
print word[1:] + word [0] + pyg # print variable word, minus first letter, add first letter to end and then append pyg
else:
print 'empty' # print 'empty' if variable original does not include any charachters or has a numerical character
@le717

This comment has been minimized.

Copy link

le717 commented Aug 9, 2013

I remember this course. It was so hard to complete, mainly due to site errors. I always wanted to complete it, but never could. So I, like many others, wrote a working solution for it. Thanks for reminding me of this. :)

@stevencombs

This comment has been minimized.

Copy link
Owner Author

stevencombs commented Aug 9, 2013

I know! I saved this code because it took me sooooo long to figure this one out and didn't want to forget the solution. Who knew it was because the site had errors.

@VoraciousSpecter295

This comment has been minimized.

Copy link

VoraciousSpecter295 commented Jun 18, 2016

updating an old post. be sure to print after the first 'if' statement in case the word is actually greater than 0.

@CrasherJ

This comment has been minimized.

Copy link

CrasherJ commented Dec 1, 2016

Thanks bud for your code! Site had bugs and they're not fixing!

@xlegan

This comment has been minimized.

Copy link

xlegan commented Feb 27, 2017

In this course, there was nothing about the [1:], and about how to remove the letter of the word.
Why all these courses provide jobs for Intelligent problem solutions which need to know more than you learned at these courses themselves

@sk0x1234

This comment has been minimized.

Copy link

sk0x1234 commented Apr 21, 2017

print(str[1:]+ str[0]+ "ay" )

is this code work?

@sidmax

This comment has been minimized.

Copy link

sidmax commented Feb 17, 2018

pyg = 'ay' #create a variable that contain 'ay'

original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
new_word = word + first + pyg
new_word = new_word[1:len(new_word)]
print new_word
else:
print 'empty'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.