《笨办法学 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
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值