<?xml version="1.0" encoding="UTF-8"?><feed
  xmlns="http://www.w3.org/2005/Atom"
  xmlns:thr="http://purl.org/syndication/thread/1.0"
  xml:lang="en"
  >
  <id>http://livingcode.org/feed/atom</id>
  <updated>2009-11-13T19:33:25Z</updated>
  <title type="text">Living Code</title>
  <subtitle type="text">Programming for the Fun of It</subtitle>
  <link rel="self" type="application/atom+xml" href="http://livingcode.org/feed" />
  <link rel="alternate" href="http://livingcode.org" />
  <rights type="text">Copyright 2009</rights>
  <generator uri="http://wordpress.org/" version="2.8.6">WordPress</generator>
      <entry>
    <id>http://livingcode.org/2008/programming-problems</id>
    <title type="html"><![CDATA[Programming Problems]]></title>
    <updated>2008-02-24T06:36:55Z</updated>
    <published>2008-02-24T06:36:55Z</published>
    <author>
      <name>Dethe</name>
      <email>delza@livingcode.org</email>
<uri>http://livingcode.org/</uri>    </author>
    <link rel="replies" type="application/atom+xml" href="http://livingcode.org/2008/02/23/programming-problems/feed" thr:count="0"  />
    <link rel="alternate" href="http://livingcode.org/2008/02/23/programming-problems" />
    <category scheme="http://livingcode.org" term="Python" />
    <summary type="html"><![CDATA[
André Roberge has started a project to embed programming challenges in his Crunchy project.  Crunchy is a way to embed both Python code and an interpreter into a web page, making it great for Python education projects.  I like the idea of collecting both sources of programming challenges, and solutions to them in [...]]]></summary>
      <content type="html" xml:base="http://livingcode.org/2008/02/23/programming-problems"><![CDATA[<p><a href="http://livingcode.org/wp-content/uploads/2008/02/ruins.png" title="Ruins"><img src="http://livingcode.org/wp-content/uploads/2008/02/ruins.png" alt="Ruins" /></a></p>
<p>André Roberge has <a href="http://aroberge.blogspot.com/2008/02/99-problems-looking-for-volunteers.html">started a project</a> to embed programming challenges in his <a href="http://code.google.com/p/crunchy/">Crunchy</a> project.  Crunchy is a way to embed both Python code and an interpreter into a web page, making it great for Python education projects.  I like the idea of collecting both sources of programming challenges, and solutions to them in Python, as a way of spurring interest. Discussion around alternative solutions could get pretty interesting.  Anyway, rather than try to coordinate this through the comments on André&#8217;s blog, or via email, I created a page on the Python wiki to help organize this: <a href="http://wiki.python.org/moin/ProblemSets">Problem Sets</a>. I listed the 99 Prolog Problems that Andé pointed to originally, and a couple more sources of programming challenges I was aware of.  Hopefully more people will fill in both the sources, and some solutions.  I&#8217;ve started to put up a few solutions, but this is definitely something that would benefit from more participation.</p>
<p>If you know of a good source of programming problems (or want to create one), or want to provide some solutions of your own, or even just kibbitz, <a href="http://wiki.python.org/moin/ProblemSets">please dive in</a>!</p>
]]></content>
        </entry>
    <entry>
    <id>http://livingcode.org/2008/simple-text-adventure</id>
    <title type="html"><![CDATA[Simple text adventure]]></title>
    <updated>2008-02-23T07:39:02Z</updated>
    <published>2008-02-23T07:39:02Z</published>
    <author>
      <name>Dethe</name>
      <email>delza@livingcode.org</email>
<uri>http://livingcode.org/</uri>    </author>
    <link rel="replies" type="application/atom+xml" href="http://livingcode.org/2008/02/22/simple-text-adventure/feed" thr:count="0"  />
    <link rel="alternate" href="http://livingcode.org/2008/02/22/simple-text-adventure" />
    <category scheme="http://livingcode.org" term="Python" />
    <summary type="html"><![CDATA[
My daughter was working on her XO and said something like, &#8220;Dad, you promised to help me with Python.&#8221; It turns out she was working on her first python program, using the bare-bones IDE for the XO, Pippy. (Aside: If anyone has a suggestion for a better environment than Pippy, that runs on the XO, [...]]]></summary>
      <content type="html" xml:base="http://livingcode.org/2008/02/22/simple-text-adventure"><![CDATA[<p><a href="http://livingcode.org/wp-content/uploads/2008/02/stairs.jpg" title="Stairs down"><img src="http://livingcode.org/wp-content/uploads/2008/02/stairs.jpg" alt="Stairs down" /></a></p>
<p>My daughter was working on her <a href="http://laptop.org/laptop/">XO</a> and said something like, &#8220;Dad, you promised to help me with Python.&#8221; It turns out she was working on her first python program, using the bare-bones IDE for the XO, <a href="http://wiki.laptop.org/go/Pippy">Pippy</a>. (Aside: If anyone has a suggestion for a better environment than Pippy, that runs on the XO, I&#8217;m all ears.) I helped her with the syntax of an if statement and explained how else works, and how to quote the strings she was testing against. Looking over her shoulder I see that she&#8217;s got a minimal version of a text adventure game shaping up.  I thought this would be a good opportunity to introduce her to lists and dicts, so I put together an email for her, so she would have it to refer back to later.  Here is what I wrote:</p>
<blockquote><p>In Python there are some powerful data structures for organizing your<br />
code and information.  You can also build your own, and I&#8217;ll show you<br />
how to do that soon, but it is amazing what you can do just with what<br />
is built in.  Three of the key structures are lists, dicts (short for<br />
dictionaries), and tuples.  Tuples are similar enough to lists that I<br />
will only talk about lists and dicts for now, and we can talk about<br />
tuples later.  Also, it is important to know that when I talk about<br />
&#8220;strings&#8221; I mean text, which is a string of characters, generally<br />
surrounded by either single quotes &#8216;like this&#8217; or double quotes &#8220;like<br />
this&#8221;.</p>
<p>Dictionaries</p>
<p>Dicts are a lot like real dictionaries: there is something you use to<br />
look something else up.  With a real dictionary you use the spelling<br />
of a word to look up its definition.  With python dictionaries, you<br />
can use a wide variety of objects to look up other objects.  The<br />
objects you look up can be anything: strings, functions, even lists or<br />
other dictionaries.  The objects you use to look up with are required<br />
to be things that cannot change, but usually they are strings.</p>
<p>For convenience we call the things we use to look up with &#8220;keys&#8221; and<br />
the things that are looked up &#8220;values&#8221;.  In the case of a real<br />
dictionary, the key is the spelling of a word, and the value is the<br />
definition.</p>
<p>There are two ways to create a dict and assign it to a variable, in<br />
this example the variable is &#8220;my_lookup&#8221;.  There is the dict()<br />
constructor function, and the literal notation.</p>
<pre class="code">my_lookup = dict(one=1, two=2, three='xyzzy') # dict constructor</pre>
<pre class="code">my_lookup = {'one': 1, 'two': 2, 'three': 'xyzzy')</pre>
<p>Once we have a dict, we can change the value of the keys, add new key/<br />
value pairs, remove key/value pairs, or loop through all the keys:</p>
<p class="code">print my_lookup['one']  # prints &#8216;1&#8242;</p>
<p class="code">my_lookup['one'] = &#8216;flugelhorn&#8217; # the key &#8216;one&#8217; is now associated with the value &#8216;flugelhorn&#8217;</p>
<p class="code">my_lookup['four'] = &#8216;this is the value of the key &#8220;four&#8221;&#8216; # add a new key, my_lookup now has four key/value pairs</p>
<p class="code">del my_lookup['one'] # remove a key/value pair, my_lookup now has keys &#8216;two&#8217;, &#8216;three&#8217;, and &#8216;four&#8217;</p>
<p class="code">for key in my_lookup:<br />
print key, my_lookup[key] # will print each key with its value, in random order</p>
<p>The last point is important: dicts do not keep any specific order for<br />
their keys.  If you need to have the keys in order, you need to sort<br />
them first.  Usually this is not a problem.</p>
<p>For more information on dicts and things you can do with them: <a href="http://docs.python.org/lib/typesmapping.html" target="_blank">http://docs.python.org/lib<wbr></wbr>/typesmapping.html</a></p>
<p>If you do need ordering, then you would generally use lists.  Lists<br />
keep what you put into them in order.  They can grow or shrink as you<br />
put things in or take them out, and you can put pretty much anything<br />
into a list, including dicts or other lists.  Getting or putting an<br />
item into a list looks a lot like getting or putting an item into a<br />
dict, except the keys are always integers.  By now you probably know,<br />
or can guess, that the first item in a list is 0, followed by 1, then<br />
2, etc.  You can also count down in a list: the last item is -1, the<br />
next-to-last is -2, etc.</p>
<pre class="code"></pre>
<pre class="code">my_list = list(1,2,3,4)</pre>
<pre class="code">my_list = [1,2,3,4] # create a literal list</pre>
<p>So far my_list may not be what you expect.  What is the value of<br />
my_list[1]?</p>
<pre class="code">print my_list[1] # prints 2 because the indexes (like keys) of the list start with 0</pre>
<pre class="code">my_list = [4,8,16,32]</pre>
<pre class="code">print my_list[1] # prints 8</pre>
<p>You can change any value in a list</p>
<pre class="code">my_list[2] = "new value of two"</pre>
<p>You can add new values to the end of a list</p>
<pre class="code">my_list.append(64)</pre>
<p>And you can go through all the values of a list:</p>
<pre class="code">for value in my_list:
    print value</pre>
<p>OK, so what does all of this have to do with games?</p>
<p>Here is a simple example game.  See if you can extend it to do some<br />
more things, like dropping an item (or all items), or the &#8216;look&#8217;<br />
command to show the long description and the room&#8217;s contents. What<br />
else would be good to add?</p>
<pre class="code">'''
Simple dungeon game
'''

character = {'inventory': [], 'location': 'west room'}

dungeon = {
    'west room': {
        'short description': 'west room',
        'long description': 'a sloping north-south passage of barren rock',
        'contents': ['pail of water', 'dragon tooth'],
        'exits': {'east': 'centre room'}
    },
    'east room': {
        'short description': 'east room',
        'long description': 'a room of finished stone with high arched ceiling and soaring columns',
        'contents': [],
        'exits': {'west': 'centre room'}
    },
    'centre room': {
        'short description': 'centre room',
        'long description': 'the very heart of the dungeon, a windowless chamber lit only by the eerie light of glowing fungi high above',
        'contents': ['golden key', 'spiral hourglass'],
        'exits': {'east': 'east room', 'west': 'west room'}
    }
}

while True:
    room = dungeon[character['location']]
    command = raw_input(room['short description'] + ' &gt; ')
    command_parts = command.split(None, 1)
    verb = command_parts[0]
    obj = command_parts[-1] # if the user only types one word, both verb and obj will be the same
    if verb in ['east', 'west', 'north', 'south', 'up', 'down', 'in', 'out']:
        if verb in room['exits']:
            character['location'] = room['exits'][verb]
            room = dungeon[character['location']]
            print 'You are in', room['long description']
            for item in room['contents']:
                print 'There is a', item, 'here'
        else:
            print 'You cannot go that way'
    if verb == 'inventory':
        print 'You are carrying:'
        for item in character['inventory']:
            print '   ', item
    if verb == 'quit':
        print 'Goodbye'
        break
    if verb == 'take':
        if obj == 'all':
            if room['contents']:
                for item in room['contents'][:]: # this part: [:] makes a copy of the list so removing items works
                    print 'You pick up the', item
                    character['inventory'].append(item)
                    room['contents'].remove(item)
            else:
                print 'There is nothing to take!'
        else:
            for item in room['contents']:
                if obj in item: # does the word in obj match any part of the text of item?
                    print 'You pick up the', item
                    character['inventory'].append(item)
                    room['contents'].remove(item)</pre>
<p>That&#8217;s it for now.  I hope this was clear.  Feel free to ask me<br />
question.</p>
<p>Love,</p>
<p>&#8211;Dad</p></blockquote>
]]></content>
        </entry>
  </feed>
