1. 程式人生 > >《笨辦法學 python3》系列練習計劃——41.來自 Percal 25 號行星的哥頓人

《笨辦法學 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')

返回目錄