This week is all about strings, in particular string methods. Of course, we also get more practice with loops and choices.
Create a directory on your USB device for this lab, say, lab06, and launch IDLE.
This week, you will submit your shell window at the end of the session. Try not to close it accidentally along the way.
You will also submit a responses.txt file this week. Download this template file and use it to record any answers or predictions asked for in the exercises.
Throughout the course, we have used built-in Python functions to compute values and to interact with users. Examples include, int(), float(), ord(), chr(), print() and input(). These are all names for operations that we want to execute.
As you read in Chapter 4, Python strings offer another kind of operation, methods. Recall that we invoke methods with a "dot" syntax, like this:
>>> name = 'eugene' >>> name.count('e') 3
Python provides a number of built-in methods for manipulating the case of strings, among them:
Let's figure out what they do by trying them out. Do not look them up in your textbook. Do not look them up with Google or any other search engine. Instead, try them out in the shell.
You may copy the expressions and paste them into your shell, one at a time.
Make up some test cases of your own.
Again, do not look them up in your textbook or on-line. Try them in the shell!
Scrabble is a word game in which words are assigned a value based on the values of the characters and their position on the game board. Character values are based roughly on the frequency of the characters in common vocabulary. In the American English version, these point values are:
We now know enough to compute word values in Python. We can process the letters of the word one at a time. For each letter, we decide which point category it belongs in and add that value to a running total. For example:
Enter a word: eat The word eat is worth 3 points. Enter a word: checkmate The word checkmate is worth 22 points. Enter a word: Wallingford The word wallingford is worth 19 points.
('Wallingford' isn't a legal word in Scrabble but, hey, it's my class.)
This is a great opportunity to use the in operator with strings:
>>> four_point_letters = 'hfwvy' >>> 'a' in four_point_letters False >>> char = 'v' >>> char in four_point_letters True
You may assume that user enters only alphabetic characters. Do not assume that they enter only lowercase letters.
In Session 11, we saw code that printed items separated by commas in a string. This sort of processing for "comma-separated values" is useful for working with spreadsheets. It can be even more useful if we can do arithmetic with the items. For example:
Enter a student record: Eugene Wallingford,20,17,20 Student Eugene Wallingford has a grade of 19.0 points.
You must use a loop to process the string. You may not use Python lists or any other feature beyond what we have studied in class or Chapters 1-4.
You may use string methods such as find() if you like, or work through the string character by character. You may even use string_split.py from Session 11 as your starting point.
Make sure that your program files are complete and saved. Save your responses.txt file. Save your shell window as interactions.py.
Submit your files for grading on the electronic submission system, at lab06 -- Strings and Their Methods.
As always, make sure you see the verification screen that says The files listed above were uploaded.
If you need any help, let me know.