Chapter 8 Activity Instructions

Activity: Pig Latin Translator

 

Pig thinking in Pig LatinIn this chapter, you explored the nature of string data, learned to manage individual string characters, search and transform strings with a variety of functions, and verify a user's string inputs. We can put all of those skills to work on a "PigLatin" program!

Pig Latin is an imaginary language, built by scrambling individual words within a sentence. If you know the secret to scrambling and de-scrambling words, then you can write or speak in Pig Latin. Only someone else that also knows your secret formula will be able to easily understand you.

Our Pig Latin Formula

While there is no single, true Pig Latin formula that everyone follows, most will scramble words in a similar way. In our own unique formula, your program will convert words as follows:

  • If the first letter is a vowel ("a", "e", "i", "o", "u"), then the new word is formed by adding "\wayto the end of the original word. For example:
"aloha"  becomes "aloha\way"
"orange" becomes "orange\way"
  • If the first letter is a consonant (not a vowel), then the new word is formed by adding a backslash "\" to the endmoving a certain number of characters to the end after the slash, and then adding the "ay" afterwards. We support a 1, 2 or 3 character move as follows:
"hello" becomes "ello\hay"             # 1-character move
"bicycle" becomes "icycle\bay"         # 1-character move

"hello" becomes "llo\heay"             # 2-character move
"bicycle" becomes "cycle\biay"         # 2-character move

"hello" becomes "lo\helay"             # 3-character move
"bicycle" becomes "ycle\bicay"         # 3-character move

 

Program Overview

The main steps your program must take are described below.

  1. Your program must first prompt the user for the number of characters to move. The user input must be an integer between 1 and 3. The program should verify user input as a valid integer in this range, and loop until the user successfully enters a valid number.
  2. Your program will then prompt the user to enter a word, phrase or sentence that will be converted to Pig Latin. Convert that input string into a list of individual words by splitting on the space character.
  3. Next, for each word in the list, check the first character and apply the correct formula to create a new word. Add that new word to the Pig Latin output string.
  4. Finally, when each word has been processed, print both the original string and the new Pig Latin string to the screen.

The following sample run demonstrates how the program should work.

Please enter the number of characters to move to the end (1 - 3): one
Try again
Please enter the number of characters to move to the end (1 - 3): 4
Try again
Please enter the number of characters to move to the end (1 - 3): 1
Please enter a word, phrase or sentence: The quick brown fox jumped over the lazy dog
The quick brown fox jumped over the lazy dog
he\tay uick\qay rown\bay ox\fay umped\jay over\way he\tay azy\lay og\day 

 

Starting Code

We are providing some starting code for your PigLatin program. If you are working offline, you can copy and paste it into a "PigLatin.py" source file that you create. But if you are working with CompuScholar's auto-graded system, you will find this code waiting for you when you click on the activity link in the course interface.

# Student name

# initialize size variable
size = 0

# get and validate user size input


# get input string we want to transform
sentence = input("Please enter a word, phrase or sentence: ")

# break string into list of individual words


# initialize empty pig latin version of the input sentence
piglatin = ""

# for each word, convert and build piglatin output
               
  # convert word to lower case
  # get first character to examine
   
  # If the first letter is a vowel
    # add the letters "\way" to the end of the word. 
                 
  # else the first letter is a consonant
    # break the word into 2 pieces at the "size" index
    
    
    # build new word by starting with the last part, adding a backslash,
    # then the first part, then the characters "ay" at the end
    

  # add the new word to the end of the pig latin string, plus a space
  

# print the original and final results!

Detailed Program Requirements

Write your program by carefully following the steps listed below. For the best grade, please match the expected output exactly (including correct spelling and case of all messages). The starting code will take care of some basic tasks for you. It also contains some comments to help keep you on track.

  1. Start with a comment that contains your student name.
  2. Declare a size variable and set it equal to zero (this step is done for you). The size variable will hold the user-selected number of characters to move to the end for words that start with a consonant.
  3. Set up a "whileinput validation loop. The loop should prompt the user to enter the number of characters to move to the end. The resulting string should be converted to an integer and stored in size. The size must be between 1 and 3. Use try / except to protect against a non-integer input. If the input validation fails due to non-integer input or a number outside the allowed range, print "Try again" and prompt the user for a new input.
You can use any logic you wish to verify the user input, just make sure the correct "Try again" message is printed each time the user enters an incorrect value and that the user is prompted until successful. Sample loops to accomplish this task are demonstrated in this chapter lessons if you need help! Test your code at this point when complete and make sure it works as expected. You may want to temporarily print() the "size" variable to the screen to verify the results, but don't leave that print() statement in your code afterwards.

 

  1. Next, prompt the user to enter a word, phrase or sentence and store the result in a variable called sentence.
  2. Next, use the str.split() function to break the sentence string into a list of individual words and store the list in a variable called words. The default space character can be used as the separator.
  3. Next, initialize an empty string variable called piglatin (this step is done for you). The piglatin variable will hold the final, translated output.
  4. Now, set up a "for" loop to walk through each of the elements in the words list. Use the variable name "word" as the loop index variable. Inside the loop, take the following steps to convert the word to Pig Latin:
    1. Set the word variable equal to the lowercase version of the word. We don't want any capital letters to confuse our vowel-detection later.
    2. Create a variable named firstChar and set it equal to the first character in the word value. Remember, use square brackets [ ] and the correct numeric index to read a character from a string.
    3. "if" the firstChar is "in" the value "aeiou" (meaning, is firstChar found anywhere in the string "aeiou"?)
      1. Set the word variable equal to word plus "\way". Be sure to properly encode the backslash character! This is the first "vowel" case in our Pig Latin formula.
    4. "else" the first character is a consonant, so take these steps:
      1. Create a variable named firstPart and set it equal to a string slice that covers the first sizecharacters in the word variable.
      2. Create a variable named lastPart and set it equal to a string slice starting at the character at index size and continuing to the end of the word string.

      The illustration below demonstrates how firstPart and lastPart contents should be created on a sample word if the user's input size = 2.

    5. Illustration of first and last parts if size = 2

        Remember, a string slice [A : B] is formed by adding square brackets and two numbers with a colon in-between to the string variable name. If "A" is left out, then the slice will get the characters from the beginning "up to" B. If "B" is left out, the slice will get the characters from "A" to the end of the string.
      1. Set the word equal to the lastPart, plus the backslash character (properly encoded!), plus the firstPart, plus the characters "ay". This is the second "consonant" case in our Pig Latin formula.
      2. You may wish to temporarily add print() statements for your firstPart, lastPart and word variables. Then test several inputs to make sure your Pig Latin formula is working as expected. Always try to verify any tricky logic to make sure it is working before moving on to the rest of the program. You might even use the "pdb" debugger, if needed, to walk through your logic line-by-line. When satisfied, remove any temporary print() statements.
    6. Set the piglatin string equal to piglatin plus the word variable, plus a space character (" "). This will add the newly encoded Pig Latin word to the end of the output sentence, keeping a space in-between each new word.
  5. When the loop is complete, print both the original sentence and the new piglatin output to the screen. You are done!

 

Activity Results

The following example runs demonstrate some possible input and output combinations. Your Pig Latin translator should successfully verify user numeric input and translate the input string to Pig Latin according to our formula. Remember that all upper case letters should be converted to lowercase for the translation and final output.

Test #1

Please enter the number of characters to move to the end (1 - 3): 1
Please enter a word, phrase or sentence: Aloha nice to meet you
Aloha nice to meet you
aloha\way ice\nay o\tay eet\may ou\yay

Test #2

Please enter the number of characters to move to the end (1 - 3): 2
Please enter a word, phrase or sentence: hello
hello
llo\heay

Test #3

Please enter the number of characters to move to the end (1 - 3): 2
Please enter a word, phrase or sentence: hello
hello
llo\heay

Test #4

Please enter the number of characters to move to the end (1 - 3): 3
Please enter a word, phrase or sentence: Happy as a clam
Happy as a clam
py\hapay as\way a\way m\claay

Test #5

Please enter the number of characters to move to the end (1 - 3): oops
Try again
Please enter the number of characters to move to the end (1 - 3): 0
Try again
Please enter the number of characters to move to the end (1 - 3): 1
Please enter a word, phrase or sentence: Forescore and seven years ago
Forescore and seven years ago
orescore\fay and\way even\say ears\yay ago\way

When your program is complete and tested, submit it for grading.

Activity Rubric

If your project is being automatically graded by our system, your grade will be calculated from 0 to 100 as follows:

PointsDescription
5"while" loop used to validate input
5size variable initialized with integer conversion
5try keyword used
5except keyword used
5words variable initialize with split()
5word converted to lowercase
5firstChar variable correctly initialized
5code builds word for vowel case
5firstPart initialized correctly
5lastPart initialized correctly
5code builds word for consonant case
5transformed word added to piglatin with spacing
10Test 1 char with "The quick brown fox jumped over the lazy dog"
10Test 2 chars with "Aloha nice to meet you"
10Test 3 chars with "Call me Ishmael"
10Test input verification

Last modified: Sunday, 18 August 2019, 9:50 PM