1. 程式人生 > >笨方法學Python 習題 43: 來自 Percal 25 號行星的哥頓人(Gothons)

笨方法學Python 習題 43: 來自 Percal 25 號行星的哥頓人(Gothons)

#!usr/bin/python
# -*-coding:utf-8-*-

import random
from urllib import urlopen
import sys

WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []

PHRASES = {
    "class ###(###):":
      "Make a class named ### that is-a ###.",
    "class ###(object):\n\tdef __init__(self, ***)" :
      "class ### has-a __init__ that takes self and *** parameters.",
    "class ###(object):\n\tdef ***(self, @@@)":
      "class ### has-a function named *** that takes self and @@@ parameters.",
    "*** = ###()":
      "Set *** to an instance of class ###.",
    "***.***(@@@)":
      "From *** get the *** function, and call it with parameters self, @@@.",
    "***.*** = '***'":
      "From *** get the *** attribute and set it to '***'."
}

# do they want to drill phrases first
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
    PHRASE_FIRST = True

# load up the words from the website
for word in urlopen(WORD_URL).readlines():
    WORDS.append(word.strip())


def convert(snippet, phrase):
    class_names = [w.capitalize() for w in
                   random.sample(WORDS, snippet.count("###"))]
    other_names = random.sample(WORDS, snippet.count("***"))
    results = []
    param_names = []

    for i in range(0, snippet.count("@@@")):
        param_count = random.randint(1,3)
        param_names.append(', '.join(random.sample(WORDS, param_count)))

    for sentence in snippet, phrase:
        result = sentence[:]

        # fake class names
        for word in class_names:
            result = result.replace("###", word, 1)

        # fake other names
        for word in other_names:
            result = result.replace("***", word, 1)

        # fake parameter lists
        for word in param_names:
            result = result.replace("@@@", word, 1)

        results.append(result)

    return results


# keep going until they hit CTRL-D
try:
    while True:
        snippets = PHRASES.keys()
        random.shuffle(snippets)

        for snippet in snippets:
            phrase = PHRASES[snippet]
            question, answer = convert(snippet, phrase)
            if PHRASE_FIRST:
                question, answer = answer, question

            print question

            raw_input("> ")
            print "ANSWER:  %s\n\n" % answer
except EOFError:
    print "\nBye"
理論上程式碼執行餘下:
$ python ex41.py 
bat.bait(children)
> From bat get the bait function and call it with self and children arguments.
ANSWER:  From bat get the bait function, and call it with parameters self, children.


class Brake(object):
	def __init__(self, beef)
> class Brake has a __init__ function that takes self and beef parameters.
ANSWER:  class Brake has-a __init__ that takes self and beef parameters.


class Cow(object):
	def crook(self, cushion)
> class Cow has-a function named crook that takes self and cushion params.
ANSWER:  class Cow has-a function named crook that takes self and cushion parameters.


cast = Beetle()
> Set cast to an instance of class Beetle.
ANSWER:  Set cast to an instance of class Beetle.


cent.coach = 'appliance'
> From cent get the coach attribute and set it to appliance.
ANSWER:  From cent get the coach attribute and set it to 'appliance'.


class Destruction(Committee):
> ^D
Bye

加分習題

解釋一下返回至下一個房間的工作原理。

建立更多的房間,讓遊戲規模變大。

除了讓每個函式列印自己以外,再學習一下“文件字串(doc strings)”式的註解。看看你能不能將房間描述寫成文件註解,然後修改執行它的程式碼,讓它把文件註解打印出來。

一旦你用了文件註解作為房間描述,你還需要讓這個函式打印出使用者提示嗎?試著讓執行函式的程式碼打出使用者提示來,然後將使用者輸入傳遞到各個函式。你的函式應該只是一些 if 語句組合,將結果打印出來,並且返回下一個房間。

這其實是一個小版本的“有限狀態機(finite state machine)”,找資料閱讀了解一下,雖然你可能看不懂,但還是找來看看吧。

我的程式碼裡有一個 bug,為什麼門鎖要猜測 11 次?

常見問題回答

怎樣設計自己的遊戲故事?

你可以自己編故事,也可以從書籍或者電影裡找些簡單場景。

筆者用的Python3.6.1。經過幾次程式碼的修改,雖然不報錯了,但也無法正確執行,有看出問題的,歡迎留言討論!