Pyglatin

 

pg-1

 

In this exercise, we will learn how to program codes for the user to interface by letting them input data. First we review on the printing function. The print statement has the string (“Pig Latin”). This will print out Pig Latin on the screen.

 

 

pg-2We now will create a new print statement with a string ‘Welcome to the Pig Latin Translator!’ we have a variable original set it equal to raw(input( with the string ‘Enter a word’) placed. We print original where the user is greeted by the screen and asked to enter a word in response,

 

 

 

pg-3

Now we add an if statement because we want the user to actually input data instead of an empty answer. The if statement is that if len(original) > 0 meaning if the data the user inputs is more than a 0 characters, the data will be printed on the screen. The else statement will be a print statement when the user does not input data, no characters at all, empty will be printed.

 

pg-4

 

Another step to ensure that the user is inputting data is if they are inputting characters and not symbols or numbers. For this we add an and statement in the if statement combining a function original.isalpha(). This will not allow the user to input non-alphabetical characters.

 

snip20161220_17

 

We can include many more in function of the if statement to modify the input of the user. We can lowercase the alphabetic characters the user inputs by adding it below the if statement using the variable original with .lower() next to it. This will lowercase any uppercase characters the user may input. Another function is the slicing function which allowed the programmer to manipulate what can be added or removed from the user’s input data. For example, here we will be adding a segment for the word the user will input, pyg = ‘ay’. Another statement first=word[0 will be placed under the if statement. ]We create a new variable new_word which equals to word+first+pyg. Then we have include new_word setting equal to new_word[1:len(new_word)]. We print new_word. We finish it off with an else statement when the user types in nothing and the results is printed Empty. The word LOL is inputted which is first lowercase then the last two letters of the word are put in the beginning of the word with the first letter of the word is third with ay added in as well. Results in ollay.