Exercise 4: Text Converter

During this lab, you will create and hand in 1 file:

  • text_converter.py - This program may be completed in pairs if you wish.

Program Requirements

In high level terms, your program is going to:

  1. Welcome the user with a message and a description of each possible command.
  2. Prompt the user to enter a string.
  3. Repeatedly prompt the user for a command and take the action of that command. The possible commands are:
    • lcase — make each character in the string lowercase.
    • ucase — make each character in the string uppercase.
    • alt — alternate case in the string from one character to the next. The first character should always be uppercase, the second lowercase, the third uppercase, and so on.
    • remspace — remove all whitespace from the string.
    • 1337 — convert the string to leet-speak:
      • Replace each L or l with a 1 (numeral one)
      • Replace each E or e with a 3 (numeral three)
      • Replace each T or t with a 7 (numeral seven)
      • Replace each O or o with a 0 (numeral zero)
      • Replace each S or s with a $ (dollar sign)
    • rev — reverse order of the characters in the string.
    • restore — undo all changes and restore the string as the user entered it.
    • new — get a new string from the user for processing.
    • quit — display a message and exit the program.

Some more specific requirements:

  1. Similar to the inventory program, the text of the commands should be exactly as shown.
  2. After any command except new is entered, the resulting string should be printed. If new is the command, it should prompt for a new string.
  3. The operations are cumulative. For example, the user should be able to:
    1. Enter the phrase hello this is a test
    2. Choose command ucase → result is HELLO THIS IS A TEST
    3. Choose command remspace → result is HELLOTHISISATEST
  4. User prompts and output messages must be exactly the same as the prompts and output messages in the Example Usage section. This includes letter case (a is not the same as A) and spacing.

    Following this instruction is very important. Please ask questions if you are unsure of something. You will lose points if you do not follow the sample exactly.
  5. As before, you must comment your code — this will be worth points.
  6. You cannot use external modules.

Example Usage

User input is bold blue.

Welcome to the text converter. Your options are to:
(lcase) make each letter lowercase
(ucase) make each letter uppercase
(alt) make each letter alternate in case, with first letter capitalized
(remspace) remove all white space
(1337) convert to l337-speak
(rev) reverse the string
(new) enter a new string
(restore) replace string with last one entered
(quit) exit the program
------------------------------
Please enter a string: cs301 is my favorite class!

Action (lcase, ucase, alt, remspace, 1337, rev, new, restore, quit): rev
!ssalc etirovaf ym si 103sc

Action (lcase, ucase, alt, remspace, 1337, rev, new, restore, quit): lcase
!ssalc etirovaf ym si 103sc

Action (lcase, ucase, alt, remspace, 1337, rev, new, restore, quit): 1337
!$$a1c 37ir0vaf ym $i 103$c

Action (lcase, ucase, alt, remspace, 1337, rev, new, restore, quit): ucase
!$$A1C 37IR0VAF YM $I 103$C

Action (lcase, ucase, alt, remspace, 1337, rev, new, restore, quit): remspace
!$$A1C37IR0VAFYM$I103$C

Action (lcase, ucase, alt, remspace, 1337, rev, new, restore, quit): restore
cs301 is my favorite class!

Action (lcase, ucase, alt, remspace, 1337, rev, new, restore, quit): alt
Cs301 Is mY FaVoRiTe cLaSs!

Action (lcase, ucase, alt, remspace, 1337, rev, new, restore, quit): new
Please enter a string: I will study hard and look at the Python documentation before I go to bed.

Action (lcase, ucase, alt, remspace, 1337, rev, new, restore, quit): remspace
IwillstudyhardandlookatthePythondocumentationbeforeIgotobed.

Action (lcase, ucase, alt, remspace, 1337, rev, new, restore, quit): 1337
Iwi11$7udyhardand100ka77h3Py7h0nd0cum3n7a7i0nb3f0r3Ig070b3d.

Action (lcase, ucase, alt, remspace, 1337, rev, new, restore, quit): alt
IwI11$7uDyHaRdAnD100Ka77H3Py7h0nD0CuM3N7A7I0Nb3f0r3iG070B3D.

Action (lcase, ucase, alt, remspace, 1337, rev, new, restore, quit): restore
I will study hard and look at the Python documentation before I go to bed.

Action (lcase, ucase, alt, remspace, 1337, rev, new, restore, quit): quit
See you next time!

Full disclosure: this test run was submitted by a TA (I won't say which one).

How do we approach this?

By now you should be getting better at looking at a problem and figuring out what programming constructs to use to accomplish that. You’re free to implement this program as you wish, given that the input and output meets the specification. If you need some suggestions, take a look at the below steps for some ideas on how to approach this. After each step, test your code and make sure it is working as you intend.

  1. Add the welcome message and the initial user input prompt.
  2. Repeatedly prompt the user for commands until the user enters quit. Add checks to see which other commands were entered, and print some temporary output (like 'User entered alt') to make sure that your conditions are working correctly.
  3. Start implementing the functionality for each command. These all involve string manipulation, so a good place to start might be reading the Python string documentation (here). You may use anything from this built-in module! Specifically take a look at upper(), lower(), and len().
  4. Define a function for each command to actually manipulate the string - the function should take a string as input, process the string to create a new string, and return that new string. For now, just return the name of the command, so that when you work the function calls into your while loop, you can tell that you're calling the functions correctly.

    By using functions instead of implementing the manipulations in your while loop directly, you're beginning to modularize your code so that it's easier to understand, and nicer to look at.
  5. Now for the new stuff: how do you actually process a string? Some things to keep in mind:
    1. You can access a character of a string based on the index of the character. For example, to access the first letter in a string s, use the code s[0]. To access the fifth character, use s[4].
    2. You can access each character of a string in order - combine string indexing with a counter-controlled loop.
    3. You can add a character to the end of an existing string using the += operator: s += 'a'
    4. You can give a variable an empty string value: s = ''
    5. You may use built-in functions from the string module.

Submitting your files

As usual, you'll be handing in your lab work via the course Learn@UW dropboxes. Navigate to our 301 course page, and click the Dropbox link in the top navigation bar. You should see a dropbox for Program 4 - this is where you should hand in your text_converter.py file. (If you worked in a pair, only one person will need to hand in the code - but make sure both names are in the file.)

Note that the dropbox will close at 12 noon on 25 February, so be sure to submit your files before then.