Exercise 25: Even More Practice

We're going to do some more practice involving functions and variables to make sure you know them well. This exercise should be straight forward for you to type in, break down, and understand.

However, this exercise is a little different. You won't be running it. Instead you will import it into your python and run the functions yourself.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print word

def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(-1)
    print word

def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)

First, run this like normal with python ex25.py to find any errors you have made. Once you have found all of the errors you can and fixed them, you will then want to follow the WYSS section to complete the exercise.

What You Should See

In this exercise we're going to interact with your .py file inside the python interpreter you used periodically to do calculations.

Here's what it looks like when I do it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$ python
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ex25
>>> sentence = "All good things come to those who wait."
>>> words = ex25.break_words(sentence)
>>> words
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
>>> sorted_words = ex25.sort_words(words)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> ex25.print_first_word(words)
All
>>> ex25.print_last_word(words)
wait.
>>> wrods
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'wrods' is not defined
>>> words
['good', 'things', 'come', 'to', 'those', 'who']
>>> ex25.print_first_word(sorted_words)
All
>>> ex25.print_last_word(sorted_words)
who
>>> sorted_words
['come', 'good', 'things', 'those', 'to', 'wait.']
>>> sorted_words = ex25.sort_sentence(sentence)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> ex25.print_first_and_last(sentence)
All
wait.
>>> ex25.print_first_and_last_sorted(sentence)
All
who
>>> ^D
$

Let's break this down line by line to make sure you know what's going on:

  • Line 5 you import your ex25.py python file, just like other imports you have done. Notice you do not need to put the .py at the end to import it. When you do this you make a module that has all your functions in it to use.
  • Line 6 you made a sentence to work with.
  • Line 7 you use the ex25 module and call your first function ex25.break_words. The . (dot, period) symbol is how you tell python, "Hey, inside ex25 there's a function called break_words and I want to run it."
  • Line 8 we just type words, and python will print out what's in that variable (line 9). It looks weird but this is a list which you will learn about later.
  • Lines 10-11 we do the same thing with ex25.sort_words to get a sorted sentence.
  • Lines 13-16 we use ex25.print_first_word and ex25.print_last_word to get the first and last word printed out.
  • Line 17 is interesting. I made a mistake and typed the words variable as wrods so python gave me an error on Lines 18-20.
  • Line 21-22 is where we print the modified words list. Notice that since we printed the first and last one, those words are now missing.

The remaining lines are for you to figure out and analyze in the extra credit.

Extra Credit

  1. Take the remaining lines of the WYSS output and figure out what they are doing. Make sure you understand how you are running your functions in the ex25 module.
  2. Try doing this: help(ex25) and also help(ex25.break_words). Notice how you get help for your module, and how the help is those odd """ strings you put after each function in ex25? Those special strings are called documentation comments and we'll be seeing more of them.
  3. Typing ex25. is annoying. A shortcut is to do your import like this: from ex25 import * which is like saying, "Import everything from ex25." Programmers like saying things backwards. Start a new session and see how all your functions are right there.
  4. Try breaking your file and see what it looks like in python when you use it. You will have to quit python with CTRL-D (CTRL-Z on windows) to be able to reload it.