小编Rac*_*hel的帖子

对 Learn Python the Hard Way ex41 中的函数感到困惑?

我被这个练习的另一部分卡住了。正在编码的程序允许您钻取短语(它为您提供一段代码,您写出英文翻译)并且我对“转换”功能的工作方式感到困惑。完整代码:http : //learnpythonthehardway.org/book/ex41.html

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 = …
Run Code Online (Sandbox Code Playgroud)

python for-loop function random-sample

2
推荐指数
1
解决办法
2426
查看次数

标签 统计

for-loop ×1

function ×1

python ×1

random-sample ×1