繁体   English   中英

我的代码既不产生输出也不产生错误,代码看起来正确,如何在没有输出的情况下进行故障排除?

[英]My code neither produces output nor an error, code looks correct, how to troubleshoot with no output?

我正在尝试解决 Zed Shaw 的“Learn Python the Hard Way”中的 Ex 41。 我已经为它创建了文件。 该练习从作者的网站检索文本文件。 当我在命令行 (Ubuntu) 上运行它时,它只会让我回到没有可见输出的提示符。

我不确定如何确定问题所在。 我试过了:

  • 仔细检查代码。 据我所知,代码看起来与本书相同。

  • 相反,在 IDLE 中运行它,不产生任何输出,只是返回到提示

  • 使用 -v 运行 python(它没有产生任何结果)

  • 将 URL 更改为 https,以及

  • 验证单词列表在该 URL 上是否可用(确实如此)。

我的任何其他 python 练习仍然运行良好。 有没有办法查看更多输出(如日志文件,或强制更多输出的方法),我可以尝试找出发生了什么?

import random
from urllib import urlopen
import sys

WORD_URL = "https://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 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 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(results)

        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"

正如 jasonharper 所提到的,您的打印内容在convert内部调用,而根本没有调用。

如果您正在寻找一种更通用的调试方法来调试未发生的事情(假设您有一些非常大而复杂的脚本),我可以建议您使用以下方法:

  • print "start"放在脚本的开头,并在似乎没有处理的指令之前print "end"
  • 运行脚本以检查您的环境是否正常(您应该在输出中看到“开始” - 但如果您说输出到文件中,那么您将不得不更改该文件或改为监视文件)以及 python 是否没有'实际上并没有进入感兴趣的指令(你不应该看到“结束”——否则指令本身有问题)
  • 现在开始移动print "end" :一次将其移出条件块或循环,或移至函数定义的第一行或从函数定义移至调用它的位置。 在某些时候,您会看到“结束”输出出现(在这种情况下,检查块的条件或函数内的返回语句其他可能阻止您到达先前print "end"的位置的事情); 或者,就像在您的示例中一样,您会注意到该调用根本不在您的程序中
  • 在某些情况下,移动print "start"或除了print "end"之外可能很有用 - 但想法是相同的:您将它们彼此靠近并寻找“开始显示,结束不显示”的时刻情况变化

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM