《笨办法学python》加分习题41——我的答案

来源:互联网 发布:淘宝蓝海市场 编辑:程序博客网 时间:2024/05/16 09:02

这是我自己学习的答案,会尽力写的比较好。还望大家能够提出我的不足和错误,谢谢!

原文例题:

from sys import  exitfrom  random import randintdef death():    quips = ["You died. You kiinda suck at this.",             "Nice job, you died ...jackass.",             "Such a luser.",             "I hace a small puppy that's better at this."]    print quips[randint(0, len(quips)-1)]    exit(1)def central_corridor():    print "The Gothons of Planet Percal #25 have incaded your " \          "ship and destryed"    print "your entire crew. You are the last surviving " \          "member and your last"    print "mission is to get the neutron destruct bomb from" \          " the Weapons Aromory,"    print "put it in the bridge, and blow the ship up after" \          " getting into an "    print "Escape pod."    print "\n"    print "Yor're running down the central corridor to the" \          "Weapons Aromry when" \          "\n" \          "a Gothon jumps out, red scaly skin, dark grimy" \          " teeth, and wvil clown costume" \          "\n" \          "flowing around his hate filled boby. He's blocking " \          "the door to the" \          "\n" \          "Armory and about to pull a weapon to blast you."    action = raw_input("> ")    if action == "shoot!":        print "Quick on the draw you yank out your blaster and" \              "fire it at the Gothon." \              "\n" \              "His clown costume is flowing and moving around " \              "his body,which throws" \              "\n" \              "off your aim. Your laser hits his costume but " \              "misses him entirely. This" \              "\n" \              "completely ruins his brand new costume his mother" \              " bought him, which" \              "\n" \              "makes him fly into an insane rage and blast you" \              " repeately in the face until" \              "\n" \              "you are dea. Then he eats you."        return 'death'    elif action == "dodge!":        print "Like a world class boxer you dodge, weave, slip" \              "and slide right" \              "\n" \              "as the Gothon's blaster cranks a laser past your" \              "head." \              "\n" \              "In the middle of your artful dodge your foot slips" \              " and you" \              "\n" \              "bang your head on the metal wall and pass out." \              "\n" \              "You wake up shortly after only to die as the Gothon" \              " stomps on" \              "\n" \              "your head and eats you."        return 'death'    elif action == "tell a joke":        print "Lucky for you they made you learn Gothon insults" \              " in the academy." \              "\n" \              "You tell the one Gothon joke you know:" \              "\n" \              "Lbhe zbgure vf fb sng, jura fur fvgf nebhq " \              "gur ubhfr, fur fvgf nebhaq gur ubhfr." \              "\n" \              "The Gothon stops, tries not to laugh, then busts" \              " out laughing and can't move." \              "\n" \              "The Gothon stops, tires not to laugh, then busts " \              "out laughing and can't move." \              "While he's laughing you run up and shoot him" \              " square in the head" \              "\n" \              "putting him down, then jump through the Weapon" \              " Armory door."        return 'laser_weapon_armory'    else:        print "DOES NOT COMPUTE!"        return 'central_corridor'def laser_weapon_armory():    print "You do a dive roll into the Weapon Armory, " \          "crouch and scan the room" \          "\n" \          "for more Gothons that might be hiding. It's " \          "dead quiet, too quiet." \          "\n" \          "You stand up and run to the far side of the room" \          " and find the" \          "\n" \          "neutron bomb in its container. There's a keypad " \          "lock on the box" \          "\n" \          "and you need the code to get the bomb out. If " \          "you get the code" \          "/n" \          "wrong 10 times then the lock closes forever and " \          "you can't" \          "\n" \          "get the bomb. The code is 3 digits."    code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))    guess = raw_input("[keypad]> ")    guesses = 0    while guess != code and guesses < 10:        print "BZZZZEDDD!"        guesses += 1        guess = raw_input("[keypad]> ")    if guess == code:        print "The container clicks open and the seal breaks," \              "letting gas out." \              "\n" \              "You grab the neutron bomb and run as fast as" \              " you can to the" \              "\n" \              "bridge where you must place it in the right" \              " spot."        return 'the_bridge'    else:        print "The lock buzzes one last thme and then you hear " \              "a sickening" \              "\n" \              "melting sound as the mechanism is fused together." \              "\n" \              "You decide to sit here, and finally the Gothons blow " \              "up the" \              "\n" \              "ship from their ship and you die."        return 'death'def the_bridge():    print "You burst onto the Bridge with the neutron destruct bomb" \          "\n" \          "under your arm and surprise 5 Gothons who are trying to" \          "\n" \          "take control of the ship. Each of them has an even uglier" \          "\n" \          "clown costume than the last. They haven't pulled their" \          "\n" \          "weapons out yet, as they see the active bomb under your" \          "\n" \          "arm and don't want to set it off."    action = raw_input("> ")    if action == "throw the bomb":        print "In a panic you throw the bomb at the group of Gothons" \              "\n" \              "and make a leap for the door. Right as you drop it a" \              "\n" \              "Gothon shoots you right i the back killing you." \              "\n" \              "As you die you see another Gothon frantically try to disarm" \              "\n" \              "the bomb. You die knowing they will probably blow up when" \              "\n" \              "it goes off."        return 'death'    elif action == "slowly place tje bomb":        print "You point your blaster at the bomb under your arm" \              "\n" \              "and the Gothons put their hands up and start to sweat." \              "\n" \              "You inch backward to the door, open it, and then carefully"        print """place the bomb on the floor, pointing your blaster at it.        You then jump back through the door, punch the close button        and blast the lock so the Gothons can't get out.        Now that the bomb is placed you run to the escape pod to        get off this tin can."""        return 'escape_pod'    else:        print "DOES NOT COMPUTE!"        return "the_bridge"def escape_pod():    print """You rush through the ship desperately trying to make it to    the escape pod before the whole ship explode. It seems like    hardly any Gothons are on the ship, so your run is clear of    interference. You get to the chamber with the escape pods, and    now need to pick one to take. Some of them could be damaged    but you don't have time to look. There's 5 pods, which one    do you take?"""    good_pod = randint(1,5)    guess = raw_input("[pod #]> ")    if int(guess) != good_pod:        print "you jump into pod %s and hit the eject buttom." % guess        print """The pod escapes out into the void of space, then        implodes as the hull ruptures, crushing your body        into jam jelly."""        return 'death'    else:        print "You jump into pod %s and hit hte eject button." % guess        print """The pod easily slides out into space heading to        the planet below. As it flies to the planet, you look        back and see your ship implode then explode like a        bright star, taking out the Gothon ship at the same        time. You won!"""        exit(0)ROOMS = {    'death': death,    'central_corridor': central_corridor,    'laser_weapon_armory': laser_weapon_armory,    'the_bridge': the_bridge,    'escape_pod': escape_pod     }def runner(map, start):    next = start    while True:        room = map[next]        print "\n---------"        next = room()runner(ROOMS, 'central_corridor')

和原文有一些出入,但实际内容一致。

为了方便阅读,我个人建议阅读代码时,先将print部分忽略,再来了解代码结构。

个人使用的是pychram,可以不展开代码,稍微推荐下。

作者实际定义了6个函数和一个ROOMS的字典。

6个函数:

# 定义了一个list,就是随机下死了说些啥def death():    quips = ["You died. You kiinda suck at this.",             "Nice job, you died ...jackass.",             "Such a luser.",             "I hace a small puppy that's better at this."]    print quips[randint(0, len(quips)-1)]    exit(1)
# 将长段的print收起(以下都是这样),这就是个在巴拉巴拉的函数# 你输入的内容不同,balabala也改变一点,当然,也改变了return的内容def central_corridor():    print ...    action = raw_input("> ")    if action == "shoot!":        print ...        return 'death'    elif action == "dodge!":        print ...        return 'death'    elif action == "tell a joke":        print ...        return 'laser_weapon_armory'    else:        print "DOES NOT COMPUTE!"        return 'central_corridor'
# 10次机会去猜code,对了去bridge,错了就飞天成仙了def laser_weapon_armory():    print ...    code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))    guess = raw_input("[keypad]> ")    guesses = 0    while guess != code and guesses < 10:        print "BZZZZEDDD!"        guesses += 1        guess = raw_input("[keypad]> ")    if guess == code:        print ...        return 'the_bridge'    else:        print ...        return 'death'
# ...抉择def the_bridge():    print ...    action = raw_input("> ")    if action == "throw the bomb":        print ...        return 'death'    elif action == "slowly place tje bomb":        print ...        print ...        return 'escape_pod'    else:        print "DOES NOT COMPUTE!"        return "the_bridge"
# 逃跑成不成看这次准不准了,还记得关门def escape_pod():    print ...    good_pod = randint(1,5)    guess = raw_input("[pod #]> ")    if int(guess) != good_pod:        print "you jump into pod %s and hit the eject buttom." % guess        print ...        return 'death'    else:        print "You jump into pod %s and hit hte eject button." % guess        print ...        exit(0)
# 运行函数def runner(map, start):    next = start    while True:        room = map[next]        print "\n---------"        next = room()
# 这个字典把函数都做成变量来用了ROOMS = {    'death': death,    'central_corridor': central_corridor,    'laser_weapon_armory': laser_weapon_armory,    'the_bridge': the_bridge,    'escape_pod': escape_pod     }

加分习题:

1、看上面的注释就明白了

阅读全文
0 1