跳转到内容

选择自己的 Pyventure/练习

从维基教科书,一个开放世界的开放书本
#### 
exercises:

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 
       deep.
       pluses:  requires very few programming concepts)

Playing with strings:

Pythong strings have a lot of useful features built in.

>>> type("some string")
<type 'str'>
>>> 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-
whelming, but 

>>> "some string".upper()
'SOME STRING'

The function 'str' creates a string.  You can see was all the 'methods' are 
for a String object are by typing:

>>> help(str)  
[.... lots of help ...]

Or you can look in the Python documentation *here* 

Exercise: 

Create a string, and play with its methods.

example: 

>>> "a string".title()
'A String'
>>> b = "hello"
>>> b.islower()
True


Assigning to variables:

ans = raw_input("What is your choice?")


Exercise:

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)
    Extra credit:  

1.  What are some positive and negative preconceptions you have about 
    programming or programmers?



2.  create a random grue.  A GRUE is a villain 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.

Thus:

>>> books[1]
'To The Lighthouse'
>>> books[0]
'War and Peace'

Exercise:

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.    

DICTS

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....
华夏公益教科书