学习python艰难的方式练习41帮助

时间:2011-03-27 18:21:28

标签: python

嘿伙计们,我不明白跑步者功能上或之后的任何内容。

感谢

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 could 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')

2 个答案:

答案 0 :(得分:1)

在python中,函数是第一类,因此ROOMS对象将字符串名称映射到实际函数。 Runner传递此地图和一个开始字符串。在while循环中,我们得到下一个函数room,并调用它(room())。它执行,返回一个将成为下一个房间的字符串,并继续(最终直到某个函数调用exit

答案 1 :(得分:0)

def runner(map, start):代码行定义了一个名为runner的函数,其输入参数称为mapstart。 (与任何变量,对象或函数一样,这些名称是任意的,可以是程序员想要的任何东西。)在担心函数的作用之前,我先看看我们传递给它的是什么。

# 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')

因此,当我们查看构成此函数的其余代码时,我们知道参数map等于ROOMS字典,start是字符串{ {1}}。请注意,此字符串是princess_lives_here字典的键之一;它的值是函数ROOMS。 (有关字典here的更多信息。)

这是我评论的函数版本:

princess_lives_here()

注意:请注意# 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() 。每次执行此行时,存储在room = map[next]中的键(字符串)将传递到字典next。在此示例中,与任何给定键关联的值始终是一个函数!这就是为什么ROOMS的最后一行实际上是一个函数调用。该函数将是runner()字典中的任何值(death(),princess_lives_here(),gold_koi_pond()等)。希望它有所帮助。