Oct 25
Discuss your projects. (Jim can share them from
midterms. Grade & comments will happen over the weekend. )
Several of you worked with interactive fiction. So ... let's talk
about some ways to implement this.
1. Functions that call each other
2. One big nested "if ..." block
3. Loop with varying internal parameters
4. Finite state machine !
One of the classic algorithms of CS is the "finite state machine".
This will be much, much easier to implement with python dictionaries,
which are discussed in chapter 11, next week. So I'm going to jump
ahead and do them now.
# This is a 'dictionary' data structure.
# It's made up of pairs of things, which we call (key, value).
data_dict = { 'name': 'stuff', 'height': 3, 'numbers': (2, 20, 4) }
# This is similar to a list, but with a list the keys are integers.
# 0 1 2
data_list = [ 'zero', 'one', 'two']
print "The 0'th element of data_list is ", data_list[0]
# You get at the stuff inside a dict in the same way,
# but the key is anything. (Usually a string, but other values work.)
print "The name'th element of data_dict is", data_dict['name']
If time allows, talk about how classes could be put into this code.
Homework has been posted: first look at defining classes.