Piglatin Translator

by jnmcn in Circuits > Computers

121 Views, 0 Favorites, 0 Comments

Piglatin Translator

11.jpg

This is an easy to make, one word piglatin translator that I came across while taking Python at CodeCademy.com.

It is very easy to make and fun to play with.

As usual, I will be using Python 2. If you do not want to go through this entire Instrucable, I also have prewritten code below.

Open Python

1.jpg

The first step is to open your choice of a Python 2 shell (I prefer IDLE Python 2). Now open a New File.

The 'Piglatin Sufix'

3.jpg

The first line of code is simply just a variable that will hold the string "ay". I like to call this the 'Piglatin Sufix'.

pyg = 'ay'

Raw_input

2.jpg

Next, we need to be able to type our word, preferably without modifying the code each time. For this we will use the function 'raw_input()' and make a variable called 'name' to hold it. Since we want the code to be user friendly, we will put a string inside the parentheses for it to print. Be sure to leave a space between the last letter and the apostrophe so nothing looks squished.

name = raw_input('Type your word ')

.

The 'if' Function

5.jpg

Next, just for fun, we'll put an if function in the code. We will use the functions 'len()' and '.isalpha'. This means that if nothing is typed, or the type is not of letters, most of our code will be skipped and the computer will follow the code under 'else'.

if len(name) > 0 and name.isalpha:

'Word' Variable

4.jpg

Next we will create a variable that will hold the typed word in lower case letters.

word = name.lower()

'First Letter' Variable

6.jpg

Next we will create a variable to hold ONLY the first letter of the typed word.

first = word[0]

Putting It All Together

7.jpg

Now it's time to put all of our newly established variables together to make our new word. But to do this we need to create another variable called 'new_word'.

new_word = word[1:] + first + pyg

Print

8.jpg

Now we can print our new, translated word on the screen.


print new_word

The 'else' Function

9.jpg

The else function is there just in case the type has something other than only letters in it.

else:

If 'else' Happens

10.jpg

If the else function does take affect, we need something for the computer to do, just for fun. So we will tell the computer to print 'empty' on the screen

print 'empty'

Finished!

pig talking.png

Done! All that's left is to run the code, type your word, and hit enter. Now you can speak Piglatin fluently!

Having Trouble?

Remember, puncuation, indentation, and spacing is very critical in coding. If you have to, download the prewritten code or look at each screenshot carefully. Happy coding!