This is a lab assignment and so you will not be submitting it. However, the concepts and practice will help you on both the homework and exams so I encourage you to make a serious effort on it during class and consider finishing it outside of class.

I recommend making a folder for today’s lab in COURSES as you usually do.

Exercise 1

For each of the following snippets of code:

  1. Predict what will happen when the snippet runs and write it down
  2. Walk through their execution by clicking the Next button to check your prediction
  3. If relevant, figure out why your prediction was wrong
  4. If the existing code has errors, try fixing them

Exercise 2

You may recall previously writing a program reverse.py previously. Here is a reminder in case you have forgotten: The program receives a phrase from the user, reverses the order of each word, and then prints the result back out to the screen. The order of the words relative to one another should remain the same, but each word’s letters should be reversed, as in the following example: Input a phrase: stay warm out there yats mraw tuo ereht

As with any program, we should start out by laying out our algorithm, which at a high level might look something like this:
1. Get phrase from user.
2. Split phrase into words
3. For each word in the phrase
    * Reverse the letters
    * Combine the letters back together
    * Print the reversed word

You are going to write/rewrite this program so that it uses functions to organize the code more effectively. Your program should contain at least three functions, with the following names and parameters (given in parentheses):

  • main() should contain the high-level algorithm (Steps 1 – 3).
  • reverseAllWords(phrase) should take a string phrase as a parameter and print a string containing the same phrase with every word reversed (Steps 2 and 3).
  • reverseWord(word) should take a string word as a parameter and print a string containing the same word in reverse order (The subparts of Step 3). reverseAllWords should call this function.

Be sure to test your program on different inputs. Important tip: one benefit of modularization is the ability to test different parts of your program separately. Before writing and testing your entire program, try writing the reverseWord(word) function and test it on its own!

Here’s a couple of string functions that might be helpful to you:

  • .split() will split a string into a list of substrings that had been separated by whitespace in the original string. Note: this cannot be used to break a string into a list of each of its characters. If you want to do this, try something like strList = list(str) or just a for-loop.
  • Remember you can go backwards in a for-loop by using range() with -1 as the third argument.
  • Remember you can print without a newline or space by using end=''

Extra 1

Here are more code snippets to predict and test:

Extra 2

In addition, a great thing to do is think about programs like drafts of a paper. It’s good to get down a first draft and then revise or refactor it. Pull up some of your old lab assignments and try refactoring them to use functions, in particular, try Exercise 4 from the Mutating Lists Lab.