3
CS 170 Section _03 _ Name_ _ Lab 6 – An Acronym Generator Due: No later than the beginning of class, Wed. 10/3/12 Recall from the class the program we wrote to decode a sequence of ASCII numbers into the string that the numbers represent. We want to write a similar program that will prompt the user for a string and then print out the acronym for the string. (An acronym is a word formed form the first letters of each word of a phrase.) For example, if the user inputs the string "business/ accounting department", the output of your program should be the string "BAD". The acronym should be printed using capital letters. Here is the algorithm we will use: Step 1) Prompt the user to enter a string. Step 2) Initialize an accumulator string Step 3) Split the string into a list of strings Step 4) Loop once for each string in the list Step 5) Paste the first letter of the current string from the list onto the accumulator string Step 6) Convert the string to capital letters Step 7) Print out the string containing the acronym. 1) Write a Python statement that will prompt the user to enter a string and store it in an appropriately named variable. Destr = input( “Please enter your desired phrase/title to acronym-ate:”) *Destr= Desired String *Acronymate: The act of turning a phrase or title into an acronym. By: Mercedes Jenkins. 2) Write a Python statement to initialize a string variable so that it contains no characters. accu = “ “

An Acronym Generator for Python Language

Embed Size (px)

DESCRIPTION

Computer Science intro to Python class, Acronym Generator Worksheet with answers for python computing language.

Citation preview

Page 1: An Acronym Generator for Python Language

CS 170 Section _03_ Name_ _Lab 6 – An Acronym Generator

Due: No later than the beginning of class, Wed. 10/3/12

Recall from the class the program we wrote to decode a sequence of ASCII numbers into the string that the numbers represent. We want to write a similar program that will prompt the user for a string and then print out the acronym for the string. (An acronym is a word formed form the first letters of each word of a phrase.)

For example, if the user inputs the string "business/ accounting department", the output of your program should be the string "BAD". The acronym should be printed using capital letters. Here is the algorithm we will use:

Step 1) Prompt the user to enter a string.Step 2) Initialize an accumulator stringStep 3) Split the string into a list of stringsStep 4) Loop once for each string in the listStep 5) Paste the first letter of the current string from the list onto the accumulator stringStep 6) Convert the string to capital lettersStep 7) Print out the string containing the acronym.

1) Write a Python statement that will prompt the user to enter a string and store it in an appropriately named variable.

Destr = input( “Please enter your desired phrase/title to acronym-ate:”)*Destr= Desired String*Acronymate: The act of turning a phrase or title into an acronym. By: Mercedes Jenkins.

2) Write a Python statement to initialize a string variable so that it contains no characters.

accu = “ “

3) Write a Python statement that will take the string input by the user, split it into a list of strings and store it in another variable. Make sure the variable is named appropriately.

listrings = Destr.split(“ “)

4) Write a Python loop that will iterate over each string in the list you created in step 3. Be sure to use an appropriately named variable for the loop index.

for phrastring in listrings():

Page 2: An Acronym Generator for Python Language

5) As your Python loop iterates over each string in the list, you want to extract the first letter of the current string from the list and concatenate it to the accumulator variable. Write an assignment statement that extracts the first letter of the string stored in the loop index and stores it in a variable.

first = phrastring[0]

6) Write an assignment statement that takes the letter stored in this variable and concatenates it on the end of the accumulator string.

acroFin = acroFin+first

7) Which Python string library function converts the characters in a string to upper case?

.upper()

8) Assemble all the statements from steps 1 through 7 into a function that finds and prints appropriately labeled acronyms with the acronym in all upper case letters. Call your function "acronym_maker".

What to turn in: When you have finished with your function, save the module as "acronym_maker.py" and run the function several times until you are certain that it works correctly. Then, print out a copy of the module that contains the function, staple it underneath this lab sheet, write your name at the top of the lab sheet and turn it in. No printed module output is required.