PYTHON PROJECT: Pig Latin
Now let’s take what we’ve learned so far and write a Pig Latin translator.
\ Pig Latin is a language game, where you move the first letter of the word to the end and add “ay.” So “Python” becomes “ythonpay.” To write a Pig Latin translator in Python, here are the steps we’ll need to take:
\
- Ask the user to input a word in English.
- Make sure the user entered a valid word.
- Convert the word from English to Pig Latin.
- Display the translation result.
\
print “Welcome to the Pig Latin Translator”
INPUT!!
Next, we need to ask the user for input.
\
name = raw_input("What's your name?") print name
\ In the above example, raw_input() accepts a string, prints it, and then waits for the user to type something and press Enter (or Return).
\ In the interpreter, Python will ask:
\ What's your name?
\ Once you type in your name and hit Enter, it will be stored in name.
print “Welcome to the Pig Latin Translator!”
original = raw_input(“Enter a word: ”)
\ Next we need to ensure that the user actually typed something.
\
empty_string = "" if len(empty_string) > 0: # Run this block. # Maybe print something? else: # That string must have been empty.
\ We can check that the user’s string actually has characters!
print “Welcome to the Pig Latin Translator”
original = raw_input(“Enter a word: ”)
if len(original) > 0:
print original
else:
print “empty”
\ Now we know we have a non-empty string. Let’s be even more thorough and check that our string only contains letters.
\ Consider the following code:
\
x = "J123" x.isalpha() # This will return 'False'
\ In the first line, we create a string with letters and numbers.
\ The second line then runs the method .isalpha() which returns False since the string contains non-letter characters.
\ You can use .isalpha() to check that a string doesn’t contain any non-letter characters! For example:
\ Use and to add a second condition to your if statement. In addition to your existing check that the string contains characters, you should also use .isalpha() to make sure that it only contains letters.
\ Don’t forget to keep the colon at the end of the if statement!
print “Welcome to the Pig Latin Translator”
original = raw_input(“Enter a word: ”)
if len(original) > 0 and original.isalpha():
print original
else:
print “empty”
\ Check and run your code a few times!
\ Now we can get ready to start translating to Pig Latin! Let’s review the rules for translation:
\ You move the first letter of the word to the end and then append the suffix ‘ay’. Example: python -> ythonpay
\ Let’s create a variable to hold our translation suffix.
\ Create a variable named pyg and set it equal to the suffix 'ay'.
\ Let’s simplify things by making the letters in our word lowercase.
\
the_string = "Hello" the_string = the_string.lower()
\ The .lower() function does not modify the string itself, it simply returns a lowercase-version. In the example above, we store the result back into the same variable.
\ We also need to grab the first letter of the word.
\
first_letter = the_string[0] second_letter = the_string[1] third_letter = the_string[2]
\ Remember that we start counting from zero, not one, so we access the first letter by asking for [0].
pyg = 'ay'
print "Welcome To The Pig Latin Translator!"
original = raw_input('Enter a word:')
word = original.lower()
first = word[0]
if len(original) > 0 and original.isalpha():
print original
else:
print 'empty'
\ Now that we have the first letter stored, we need to add both the letter and the string stored in pyg to the end of the original string.
\ Remember how to concatenate (i.e. add) strings together?
\
greeting = "Hello " name = "D. Y." welcome = greeting + name
pyg = 'ay'
print "Welcome To The Pig Latin Translator!"
original = raw_input('Enter a word:')
word = original.lower()
first = word[0]
new_word = word + first + pyg
if len(original) > 0 and original.isalpha():
print original
else:
print 'empty'
\ Well done! However, now we have the first letter showing up both at the beginning and near the end.
\
s = "Charlie"
\ print s[0]
#will print "C"
\ print s[1:4]
#will print "har"
\
- First we create a variable s and give it the string "Charlie"
- Next we access the first letter of "Charlie" using s[0]. Remember letter positions start at 0.
- Then we access a slice of "Charlie" using s[1:4]. This returns everything from the letter at position 1 up till position 4.
\ We are going to slice the string just like in the 3rd example above.
\ Set new_word equal to the slice from the 1st index all the way to the end of new_word. Use [1:len(new_word)] to do this.
pyg = 'ay'
print "Welcome To The Pig Latin Translator!"
if len(original) > 0 and original.isalpha():
print “Here’s your translation!”
else:
print “Please type in a word! Make sure that there are no numbers!”
original = raw_input('Enter a word:')
word = original.lower()
first = word[0]
new_word = word + first + pyg
final = new_word[1:len(new_word)]
print final
\ Yay! You should have a fully functioning Pig Latin translator. Test your code thorougly to be sure everything is working smoothly.
\ You’ll also want to take out any print statements you were using to help debug intermediate steps of your code. Now might be a good time to add some comments too! Making sure your code is clean, commented, and fully functional is just as important as writing it in the first place.
pyg = 'ay'
print "Welcome To The Pig Latin Translator!"
#this takes the word typed and checks if it's actually a word
original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
print "Here's your translation!"
else:
print "Please type in a word! make sure there are no numbers!"
#turns it into lowercase
word = original.lower()
#variable with only the first letter
first = word[0]
#this will give the translation with the first letter still there
new_word = word + first + pyg
#prints only from the 2nd letter onwards
final = new_word[1:len(new_word)]
print final
\