开发者

learn python the hard way exercise 41 help

hey guys, i dont understand what anything on or after the runner function.

thanks

from sys import exit
from random import randint

def death():
    quips = ['you died. you kinda suck at this.', 'your mum would be proud of you, if she where smarter.', 'such a luser', 'i have a small puppy that\'s better than you']
    print quips[randint(0, len(quips)-1)]
    exit(1)

def princess_lives_here():
    print 'you see a beautiful princess with a shiny crown.'
    print 'she offers you some cake.'

    eat_it = raw_input('> ')

    if eat_it == 'eat it':
        print 'you explode like a pinata full of frogs.'
        print 'the princess cackles and eats the frogs. yum!'
        return 'death'

    elif eat_it == 'do not eat it':
        print 'she throws the cake at you and it cuts your head off'
        print 'the last thing you she is her eating your torso. yum!'
        return 'death'

    elif eat_it == 'make her eat it':
        print 'she screams as you cram the cake into her mouth.'
        print 'then she smiles and cries and thanks you for saving her.'
        print 'she points to a tiny door and says \'the koi needs cake too.\''
        print 'she gives you the very last bit of cake a shoves you in.'
        return 'gold_koi_pond'

    else:
        print 'the princess looks at you confused and points to the cake'
        return 'princess_lives_here'

def gold_koi_pond():
    print 'there is a garden with a koi pond in the center.'
    print 'you walk close and see a massive fin poke out.'
    print 'you peek in and see a huge creepy koi stares at you.'
    print 'it opens it\'s mouth waiting for food.'

    feed_it = raw_input('> ')

    if feed_it == 'feed it':
        print 'the koi jumps up, and rather than eating the cake, eats your arm'
        print 'you fall in and the koi eats you'
        print 'the are then pooped out some time later.'
        return 'death'

    elif feed_it == 'do not feed it':
        print 'the koi grimaces, then thrashes around for a second.'
        print 'it rushes to the other end of the and braces itself against the wall...'
        print 'then it *lunges* out the water, into the air and over your'
        print 'entire body, cake and all.'
        print 'you are then pooped out a week later'
        return 'death'

    elif feed_it == 'throw it in':
        print 'the koi wiggles, then jumps into the air and eats it.'
        print 'you can see its happy, it then grunts, thrashes...'
        print 'and finally rolls over and poops a magic diamond into the air.'
        print 'at your feet.'

        return 'bear_with_sword'

    else:
        print 'the koi wiggles a bit, annoyed.'
        return 'gold_koi_pond'

def bear_with_sword():
    print 'puzzled, you are about to pick up the fish poop diamond when'
    print 'a bear bearing a load bearing sword walks in...'
    print '"Hey! That\'s my diamond, where\'d you get it?"'
    print 'it holds it\'s paw out and looks at you.'

    give_it = raw_input('> ')

    if give_it == 'give it':
        print 'the bear swipes at your hand to grab the diamond and'
        print 'rips your hand off in the process. it then looks at'
        print 'your bloody stump and says, "oh crap, sorry about that".'
        print 'it tries to put your hand back on but you collapse.'
        print 'the last thing you see is the bear shrug and eat you.'
        return 'death'

        elif give_it == 'say no':
        print 'the bear looks shocked. nobody ever tolf a bear'
        print 'with a with a broadsword \'no\'. it ask\'s, '
        print '"is it because it\'s not a katana? i coul开发者_JAVA百科d get one!!"'
        print 'it then runs off and you notice a big iron gate.'
        print '"where the hell did that come from?" you say.'
        return 'big_iron_gate'

    else:
        print 'the bear looks puzzled as to why you\'d do that'
        return 'bear_with_sword'

def big_iron_gate():
    print 'you walk upto the big iron gate and you notice it has a handle'

    open_it = raw_input('> ')

    if open_it == 'open it':
        print 'you open it and you are free!'
        print 'there are mountains! and berries! and...'
        print 'oh, but then the bear comes holding his kataba and stabs you.'
        print '"who\'s laughing now?! love this katana!"'

        return 'death'

    else:
        'that doesn\'t seem sensible'

ROOMS = {'death': death,
        'princess_lives_here': princess_lives_here,
        'gold_koi_pond': gold_koi_pond,
        'big_iron_gate': big_iron_gate,
        'bear_with_sword': bear_with_sword
        }

def runner(map, start):
    next = start

    while True:
        room = map[next]
        print '\n------------'
        next = room()

runner(ROOMS, 'princess_lives_here')


In python, functions are first class, so the ROOMS object maps string names to actual functions. Runner is passed this map, and a start string. In the while loop, we get the next function, room, and call it (room()). It executes, returns a string which will be the next room, and continues (ultimately until some function calls exit


The def runner(map, start): line of code is defining a function called runner with input parameters called map and start. (As with any variable, object or function, these names are arbitrary and can be anything the programmer wants.) Before worrying what the function does, I'd take a look at what we pass to it.

# Call the runner function. We pass in the `ROOMS` dictionary (defined 
# on line 114) and a text string as input parameters.
runner(ROOMS, 'princess_lives_here')

Therefore, when we look at the rest of the code that makes up this function, we know that the parameter map is equal to the ROOMS dictionary and start is the string princess_lives_here. Note that this string one of the keys to the ROOMS dictionary; its value is the function princess_lives_here(). (More about dictionaries here.)

Here's my commented version of the function:

# Define a function called runner with input parameters called map and start
def runner(map, start):                    
    # next gets the value of start ('princess_lives_here')
    next = start                           

    # Create a while loop that will go on until exit() gets called.
    while True:                           
        # room gets the value of map which equals ROOMS['princess_lives_here'] 
        # the 1st time through this loop. 
        room = map[next]                  

        # Print some strings.
        print '\n------------'

        # Finally, call the function stored in room. 
        # The function is princess_lives_here() the 1st time through this loop.
        next = room()

NOTE: Pay attention to room = map[next]. Each time this line executes, the key (a string) stored in next gets passed to the dictionary ROOMS. In this example, the value associated with any given key is always a function! That is why the last line of runner() is actually a function call. The function will be any of the values in the ROOMS dictionary (death(), princess_lives_here(), gold_koi_pond(), etc.). Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜