《笨办法学 python3》系列练习计划——41.来自 Percal 25 号行星的哥顿人

题目

在本题中 Zed 首先为我们讲解了如何理解 40 题中最后两行留下的小尾巴,让后顺便又为我们介绍了两种解读代码的方法,而后的练习我觉得更像是为了让我们实践这三种解读方法而设立的。

cities['_find'] = find_city
city_found = cities['_find'](cites, state)

还记得这段“小尾巴”么?我们先看第一种正序解读

正序解读,由前向后阅读代码

首先要记得一点就是函数也是可以当作变量来使用的。所以 def find_city 实际上也创建了一个可以随意调用的变量。

而第一行代码也很好理解,就是创建了一个名为 cities 的字典,为其中名字是 '_find' 的键赋值,值为函数 find_city
第二行可以拆分为 7 步:

  1. 运行到 city_found = 时 python 知道需要创建一个变量了。
  2. 当它读到 cities 时知道了这是一个字典。
  3. 而后紧接着读到 ['_find'] 就知道了这是字典的索引,于是就通过索引找到了 cities 中的对应位置,并且读取了其中的值。
  4. ['_find'] 对应的值是函数 find_city ,所以 python 也就知道了这里是一个函数,于是当接下来马上遇到 ( 的时候就知道了这里要调用函数了。
  5. find_city 接受两个值,由 (cities, state) 传入,并运行函数。
  6. 于是从 cities 中查找是否包含 state ,如果包含就返回找到了内容,否则就返回一个什么也没找到的信息。
  7. 最后 find_city 函数返回一个信息,并将这个信息赋值给最开始的变量 city_found

倒序解读,向后阅读代码

Zed 说这种方法更容易理解代码的意义,是不是这样呢?剧透一下,反正我觉得比最后一种简单。

  1. statecities ……它们是什么呢?
  2. 是参数,传递给……
  3. 一个函数,位置在……
  4. '_find' 然后寻找,目的地是……
  5. cities 这个位置……
  6. 最后赋值给 city_found

由内向外阅读代码

最后这种方法是由内向外阅读的,大家来感受一下

  1. 找到表达式的中心位置,此处是 ['_find']
  2. 逆时针追溯,首先看到字典 cities ,这样就知道了 cities 中的 _find 元素。
  3. 上一步得到了一个函数,继续逆时针寻找,看到了参数。
  4. 参数传递后,函数会得到一个值。然后再逆时针寻找。
  5. 最后,我们看到了 city_found = 的赋值位置,知道了了函数返回值要赋值过来。于是得到了最终结果。

以上就是这三种方法了,大神肯定用不到了,不过我们小菜菜们可以选择一种适合自己和方法去理解代码。

加分练习

  1. 解释一下返回至下一个房间的工作原理。
  2. 创建更多的房间,让游戏规模变大。
  3. 除了让每个函数打印自己以外,再学习一下“文档字符串(doc strings)”的注解。看看能不能将房间描述写成注解,然后修改运行它的代码,让它把文档注解打印出来。
  4. 一旦用文档注解作为房间描述,你还需要让这个函数打印出用户提示么?试着让运行函数的代码打出用户提示来,然后将用户输入传递到各个函数。你的函数应该只是一些 if 语句组合,将结果打印出来,并且返回一下房间。
  5. 这其实是一个小版本的“有限状态机(finite state machine)”,找资料阅了解一下,虽然可能看不懂,但还是看看。




我的答案

41.0 基础答案

from sys import exit    # sys.exit 用于结束程序
from random import randint    # random.randint 获得一个随机整数。


def death():
    quips = ["You died. You kinda suck at this.",
             "Nice job, you died ...jackass.",
             "Such a luser.",
             "I have 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 invaded your ship and destroyed")
    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 Armory,")
    print("put it in the bridge, and blow the ship up after getting into an")
    print("escape pod.")
    print("\n")
    print("You're running down the central corridor to the Weapons Armory when")
    print("a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clown costume")
    print("flowing around his hate filled body. He's blocking the door to the")
    print("Armory and about to puul a weapon to blast you.")

    action = input("> ")

    if action == "shoot!":
        print("Quick on the draw you yank out your blaster and fire it at the Gothon.")
        print("His clown costume is flowing and moving around his body, which throws")
        print("off your aim. Your laser hits his costume but misses him entirely. This")
        print("completely ruins his brand new costume his mother bought him, which")
        print("makes him fly into an insane rage and blast you repeatedly in the face until")
        print("you are dead. Then he eate you.")
        return 'death'

    elif action == "dodge!":
        print("Like a world class boxer you dodge, weave, slip and slide right")
        print("as the Gothon's blaster cranks a laser past your head.")
        print("In the middle of your artful dodge your foot slips and you")
        print("bang your head on the metal wall and pass out.")
        print("You wake up shortly after only to die as the Gothon stomps on")
        print("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")
        print("You tell the one Gothon joke your know:")
        print("Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr.")
        print("The Gothon stops, tries not to laugh, then busts out laughing and can't move.")
        print("While he's laughing you run up and shoot him square in the head")
        print("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")
    print("for more Gothons that might be hiding. It's dead quiet, too quiet.")
    print("You stand up and run to the far side of the room and find the")
    print("neutron bomb in its container. There's a keypad lock on the box")
    print("and you need the code to get the bomb out. If you get the code")
    print("wrong 10 times then the lock closes forever and you can't")
    print("get the bomb. The code is 3 digits.")
    code = "%d%d%d" %(randint(1,9), randint(1,9), randint(1,9))
    guess = input("[keypad]> ")
    guesses = 0

    while guess != code and guesses < 10:
        print("BZZZZEDDD!")
        guesses += 1
        guess = input("[keypad]> ")

    if guess == code:
        print("The container clicks open and the seal breaks, letting gas out.")
        print("you grab the neutron bomb and run as fast as you can to the")
        print("bridge where you must place it in the right spot.")
        return 'the_bridge'

    else:
        print("The lock buzzes one last time and then you hear a sickening")
        print("melting sound as the mechanism is fused together.")
        print("You decide to sit there, and finally the Gothons blow up the")
        print("ship from their ship and you die.")
        return 'death'


def the_bridge():
    print("You burst onto the Bridge with the neutron destruct bomb")
    print("under your arm and surprise 5 Gothons who are trying to")
    print("take control of the ship. Each of them has an even uglier")
    print("clown costume than the last. They haven't pulled their")
    print("weapons out yet, as they see the active bomb under your")
    print("arm and don't want to set it off.")

    action = input("> ")

    if action == "throw the bomb":
        print("In a panic you throw the bomb at the group of Gothons")
        print("and make a leap for the door. Right in the back killing you.")
        print("As you die you see another Gothon frantically try to disarm")
        print("the bomb. You die knowing they will probably blow up when")
        print("it goes off.")
        return 'death'

    elif action == "slowly place the bomb":
        print("You point your blaster at the bomb under your arm")
        print("and the Gothons put their hands up and start to sweat.")
        print("You inch backward to the door, open it, and then carefully")
        print("place the bomb on the floor, pointing your blaster at it.")
        print("You then jump back through the door, punch the close button")
        print("and blast the lock so the Gothons can't get out.")
        print("Now that the bomb is placed you run to the escape pod to")
        print("get off this tin can.")
        return 'escape_pod'
    else:
        print("DSES NOT COMPUTE")
        return "the_bridge"


def escape_pod():
    print("You rush through the ship desperately trying to make it to")
    print("the escape pod before the whole ship explodes. It seems like")
    print("hardly any Gothons are on the ship, so your run is clear of")
    print("interference. You get to the chamber with the escape pods, and")
    print("now need to pick noe to take. Some of them could be damaged")
    print("but you don't have time to look. There's 5 pods, which one")
    print("do you take?")

    good_pod = randint(1,5)
    guess = input("[pod #]> ")


    if int(guess) != good_pod:
        print("You jump into pod %s and hit the eject button." % guess)
        print("The pod escapes out into the void of space, then")
        print("implodes as the hull ruptures, crushing your body")
        print("into jam jelly.")
        return 'death'
    else:
        print("You jump into pod %s and hit the eject button." % guess)
        print("The pod easily slides out into space heading to")
        print("the planet below. As it flies to the planet, you look")
        print("back and see your ship implode then explode like a")
        print("bright star, taking out the Gothon ship at the same")
        print("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')

这道练习题除了大量的文本之外,整体的难点只有最后的 runner 函数,理解这个函数是理解整个程序是如何工作的关键点,我将在 41.1 的答案中尝试分析。

41.1 解释返回房间的工作原理

Zed 在本题教了我们 3 种分析语句的方法,这里就是实践的地方了。
本题练习的难点就是最后的 runner 函数,所有我想尝试用 1 种方法尝试解读。

正序解读

  1. def runner(map, start): 定义函数 runner 并接受两个参数:map start
    1. next = 定义局部变量 next 其值是函数 runner 的其中一个参数 start
    2. 执行 while 循环,循环条件永远为真 True
      1. 循环体中 room = 定义局部变量 room,其值是函数 runner 的另一个参数 map ,赋值时对参数 map 执行 [next] (查询索引值)操作。
      2. 在循环体内,打印:换行符和若干减号
      3. 在循环体内,next = 重新为变量 next 赋值,它的值是将变量 room 以函数方式执行后的结果。
  2. 函数定义完毕。
  3. runner( 执行函数 runner 其参数是字典 ROOMS 和字符串 ‘central_corridor'
    1. 函数 runner 执行中,next 变量的值是字符串参数 ’central_corridor'
    2. 函数的 while 循环执行。
      1. 中变量 room 的值是字典 ROOMS 的索引 next 的值,也就是 ROOMS['central_corridor'] ,其值实际指向函数 central_corridor 但是没有括号所以没有执行。
      2. 打印
      3. 局部变量 next 再次赋值,值是执行变量 room 中的函数后取得的值。即执行函数 central_corridor()。根据函数执行情况 next 的值可能是字符串 ‘death’,也可能是字符串 'laser_weapon_armory'
    3. while 再次执行。
      1. 其中局部变量 room 的值可能是字典 ROOMS[‘death’]['laser_weapon_armory'] 的索引值。也就是函数 death 或函数 laser_weapon_armory
      2. 打印
      3. 为局部变量 next 赋值,其值是函数执行结果。如果执行的函数是 death() 则最终触发 sys.exit(1) 退出程序。如果执行的是 laser_weapon_armory 则进入下一个房间。
    4. 后续循环类似。

41.3 文档字符串(doc strings)

文档字符串是定义在函数首行的一个多行字符串,用于说明函数的功能、参数、返回值等函数有关的介绍信息。

和定义其他信息一样,python 的文档字符串拥有自己的格式:

  1. 书写位置通常固定在函数定义的第一行开始。
  2. 使用三引号 ''' """ 标记起始和结束。
  3. 第一行通常是函数的简介。
  4. 第二行通常留空。
  5. 第三行开始书写具体的介绍。

在使用时主要有两种使用方法。

  1. 使用 help(函数名) 调用,它的形式和查询 python 的帮助文档是一样的,使用 q 退出。
  2. 使用 __doc__ 调用,注意这里前后都是双下划线。例如 print(函数名.__doc__) 可以打印出文档字符串。

我改造后的代码如下,为了对齐,把一些字符串的前面加了 4 个空格,另外由于程序本身的使用需要,并没有遵循文档字符串的格式。

# 将 41 题文档字符串话改造


from sys import exit    # sys.exit 用于结束程序
from random import randint    # random.randint 获得一个随机整数。


def death():
    quips = ["You died. You kinda suck at this.",
             "Nice job, you died ...jackass.",
             "Such a luser.",
             "I have a small puppy that's better at this."]

    print(quips[randint(0, len(quips)-1)])
    exit(1)


def central_corridor():
    """
    The Gothons of Planet Percal #25 have invaded your ship and destroyed
    your entire crew. You are the last surviving member and your last
    mission is to get the neutron destruct bomb from the Weapons Armory,
    put it in the bridge, and blow the ship up after getting into an
    escape pod.")

    You're running down the central corridor to the Weapons Armory when
    a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clown costume
    flowing around his hate filled body. He's blocking the door to the
    Armory and about to puul a weapon to blast you.
    """

    action = input("    > ")

    if action == "shoot!":
        print("    Quick on the draw you yank out your blaster and fire it at the Gothon.")
        print("    His clown costume is flowing and moving around his body, which throws")
        print("    off your aim. Your laser hits his costume but misses him entirely. This")
        print("    completely ruins his brand new costume his mother bought him, which")
        print("    makes him fly into an insane rage and blast you repeatedly in the face until")
        print("    you are dead. Then he eate you.")
        return 'death'

    elif action == "dodge!":
        print("    Like a world class boxer you dodge, weave, slip and slide right")
        print("    as the Gothon's blaster cranks a laser past your head.")
        print("    In the middle of your artful dodge your foot slips and you")
        print("    bang your head on the metal wall and pass out.")
        print("    You wake up shortly after only to die as the Gothon stomps on")
        print("    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")
        print("    You tell the one Gothon joke your know:")
        print("    Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr.")
        print("    The Gothon stops, tries not to laugh, then busts out laughing and can't move.")
        print("    While he's laughing you run up and shoot him square in the head")
        print("    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():
    """
    You do a dive roll into the Weapon Armory, crouch and scan the room
    for more Gothons that might be hiding. It's dead quiet, too quiet.
    You stand up and run to the far side of the room and find the
    neutron bomb in its container. There's a keypad lock on the box
    and you need the code to get the bomb out. If you get the code
    wrong 10 times then the lock closes forever and you can't
    get the bomb. The code is 3 digits.
    """
    code = "%d%d%d" %(randint(1,9), randint(1,9), randint(1,9))
    guess = input("    [keypad]> ")
    guesses = 0

    while guess != code and guesses < 10:
        print("BZZZZEDDD!")
        guesses += 1
        guess = input("    [keypad]> ")

    if guess == code:
        print("    The container clicks open and the seal breaks, letting gas out.")
        print("    you grab the neutron bomb and run as fast as you can to the")
        print("    bridge where you must place it in the right spot.")
        return 'the_bridge'

    else:
        print("    The lock buzzes one last time and then you hear a sickening")
        print("    melting sound as the mechanism is fused together.")
        print("    You decide to sit there, and finally the Gothons blow up the")
        print("    ship from their ship and you die.")
        return 'death'


def the_bridge():
    """
    You burst onto the Bridge with the neutron destruct bomb
    under your arm and surprise 5 Gothons who are trying to
    take control of the ship. Each of them has an even uglier
    clown costume than the last. They haven't pulled their
    weapons out yet, as they see the active bomb under your
    arm and don't want to set it off.
    """

    action = input("    > ")

    if action == "throw the bomb":
        print("    In a panic you throw the bomb at the group of Gothons")
        print("    and make a leap for the door. Right in the back killing you.")
        print("    As you die you see another Gothon frantically try to disarm")
        print("    the bomb. You die knowing they will probably blow up when")
        print("    it goes off.")
        return 'death'

    elif action == "slowly place the bomb":
        print("    You point your blaster at the bomb under your arm")
        print("    and the Gothons put their hands up and start to sweat.")
        print("    You inch backward to the door, open it, and then carefully")
        print("    place the bomb on the floor, pointing your blaster at it.")
        print("    You then jump back through the door, punch the close button")
        print("    and blast the lock so the Gothons can't get out.")
        print("    Now that the bomb is placed you run to the escape pod to")
        print("    get off this tin can.")
        return 'escape_pod'
    else:
        print("    DSES NOT COMPUTE")
        return "the_bridge"


def escape_pod():
    """
    You rush through the ship desperately trying to make it to
    the escape pod before the whole ship explodes. 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 noe 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 = input("    [pod #]> ")


    if int(guess) != good_pod:
        print("    You jump into pod %s and hit the eject button." % guess)
        print("    The pod escapes out into the void of space, then")
        print("    implodes as the hull ruptures, crushing your body")
        print("    into jam jelly.")
        return 'death'
    else:
        print("    You jump into pod %s and hit the eject button." % guess)
        print("    The pod easily slides out into space heading to")
        print("    the planet below. As it flies to the planet, you look")
        print("    back and see your ship implode then explode like a")
        print("    bright star, taking out the Gothon ship at the same")
        print("    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-------------")
        print(room.__doc__)
        next = room()

runner(ROOMS, 'central_corridor')




返回目录

《笨办法学 python3》系列练习计划——目录

你可能感兴趣的:(Python)