1. make some more branches, using the if-else construct we've seen so far
What are some positive and negative aspects to this approach?
(bad, hard to handle nesting, lots of code, hard to read when it gets
pluses: requires very few programming concepts)
Playing with strings:
Pythong strings have a lot of useful features built in.
>>> type("some string")
>>> dir("some string")
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__',
'__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__',
'__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',
'__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith',
'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex',
rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip',
'swapcase', 'title', 'translate', 'upper', 'zfill']
Some of these are going to look like gibberish, and the number of them is over-
>>> "some string".upper()
The function 'str' creates a string. You can see was all the 'methods' are
for a String object are by typing:
[.... lots of help ...]
Or you can look in the Python documentation *here*
Create a string, and play with its methods.
>>> "a string".title()
>>> b = "hello"
Assigning to variables:
ans = raw_input("What is your choice?")
Write an "Angry Boss" program that rudely asks what you want. No matter what your answer, it should repeat it back and tell you you're fired.
If / elif / else
1. Ask the player their name as the first action of the game. Refer to them
by name throughout.
1. Expand your input parser to accept other values beside 'yes' and 'no'.
Specifically, allow "y", "n". (Hint: use the 'or' construct, or more
advanced, just check the first letter of the word)
1. What are some positive and negative preconceptions you have about
programming or programmers?
2. create a random grue. A GRUE is a villian from the original ZORK game.
In that interactive fiction game, the GRUE appeared to eat the player
whenever there was darkness. Our GRUE will be a little more forgiving,
and will appear sometimes.
(import random ; imports)
make it possible to run in multiple rooms
Add a "hidden" "answer" style to the wikibook?
## data types
As we saw when we tried to expand out the map, it gets tedious if we have to
use nested if statements.
Lists and dicts
A LIST in python has:
0 or more elements
that are stored *with their order*
Real life example: Books on a bookshelf.
books = ['War and Peace', 'To the Lighthouse', 'Think Python', "White Teeth"]
We can access any item in the list easily by their 'index', that is, their
position in the order. The numbering starts from 0 [cf: computer history callout].
The reasons are historical, and it's a little odd, but for now, just accept it.
'To The Lighthouse'
'War and Peace'
1. Conceptually, how would you find a particular book in this list?
(try to implement this if you like, but note that there are ways of doing
this in python. This is a good chance to talk about algorithmic complexity
1. Try playing with other indexes. Try using letters, or negative numbers, or
numbers bigger than 4. What happens? Use your experiences to give a more
complete model of Python lists, and list indexing.
So, sometimes, keeping things in order is pointless. Sometimes we want to be
able to associate data with names or labels.
1. Try indexing our dict 'authors' by ...
Unknowingly, we've been using lists all over the place. In python, strings
are implemented as lists internally.
## What is a program
A program is a series of commands....