【问题标题】:NameError: name 'text' is not defined when importing new function named 'text'NameError:导入名为“text”的新函数时未定义名称“text”
【发布时间】:2019-05-27 05:54:55
【问题描述】:
Traceback (most recent call last):
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python37-32\ch6\analyze.py", line 46, in <module>
    words = text.split()
NameError: name 'text' is not defined

我已经检查了我的代码,以确保我按照我所关注的书的状态输入它。我正在努力学习 Head First Learn to Code。一切似乎都应该如此。我不知道为什么我的代码总是抛出这个错误。

import ch1text

def count_syllables(words):
    count = 0

    for word in words:
        word_count = count_syllables_in_word(word)
        count = count + word_count

    return count

def count_syllables_in_word(word):
    count = 0

    if len(word) <= 3:
        return 1

    vowels = "aeiouAEIOU"
    prev_char_was_vowel = False

    for char in word:
        if char in vowels:
            if not prev_char_was_vowel:
                count = count + 1
            prev_char_was_vowel = True
        else:
            prev_char_was_vowel = False

    return count

def count_sentences(text):
    count = 0

    for char in text:
        if char == '.' or char == ';' or char == '?' or char == '!':
            count = count + 1

        return count

def compute_readability(text):
    total_words = 0
    total_sentences = 0
    total_syllables = 0
    score = 0

words = text.split()
total_words = len(words)
total_sentences = count_sentences(text)
total_syllables = count_syllables(words)

print(total_words, 'words')
print(total_sentences, 'sentences')
print(total_syllables, 'syllables')

compute_readability(ch1text.text)

还有一个文本文件,它最初是一个 ch1text.txt 文件,我被告知在 Python 中打开它,然后在将其导入 Python 之前将其保存为 ch1text.py。当我运行 ch1text.py 文件时,它会运行文本。我期待的是另一个代码来阅读我导入的文本并告诉我 ch1text.py 中有多少单词、句子和音节。相反,我收到错误消息说未定义文本。请帮我。我花了很长时间试图自己解决这个问题,并且在我明白自己哪里出错之前我觉得我无法继续前进。

*使用适当的缩进进行编辑,以反映我在 py 文档中的内容。

【问题讨论】:

  • 请修正缩进。 Python 中的缩进是一个句法 特性。您发布的代码有许多语法错误,其中一些可能是您看到的错误的原因。

标签: python string text


【解决方案1】:

问题在于函数调用。在您正在编写的代码中,

words = text.split() <--
total_words = len(words)
total_sentences = count_sentences(text) <--

如果你仔细观察的话,text 并不是一个被传递给 text.split 或 count_sentences 的全局变量。所以你需要解决这个问题。您只能拆分存在的东西,在这种情况下不存在。

【讨论】:

  • 另外一个文档,按照书上说放到Python里的叫ch1text,变成了应该用这段代码导入的函数。
  • 应该是存储函数的文件显示 text = """在您编写第一段真实代码之间的第一件事是学习分解问题的技巧变成计算机可以为你做的可实现的小动作。当然,你和计算机也需要说一种共同的语言,但我们稍后会谈到这个话题。"""
  • 我认为当它在我共享的原始代码中显示文本时,它指的是这个文件和函数?如果没有,我该怎么做才能修复它,以便可以将 ch1text 作为函数导入以使用我发布的原始代码?
  • @ChristinaVarghese 所以克里斯蒂娜你的问题不是很清楚,但我想我应该做一些研究,看看我发现了什么:books.google.co.in/…。
  • @ChristinaVarghese 所以问题是,如果你仔细观察,你会发现缩进仍然不正确......我在答案中所说的行需要在 compute_readability(text) 方法中缩进。请参考我上面提到的链接。
【解决方案2】:

在 Python 中,缩进用于determine the grouping of statements。

您可能只需要缩进以下出现的所有行:

def compute_readability(text):

【讨论】:

  • 它们在我的 python 文档中是缩进的,但是当我在这里发布时,论坛让我将所有主行缩进 4 个空格,因此缩进关闭。我的代码看起来与书中显示的完全一样,这就是我如此困惑的原因。
  • 我们无权访问这本书。请编辑您的帖子并缩进代码,使其与您的程序中显示的完全相同。抱歉,我很迂腐,但 Python 依赖于间距来解释代码。
  • 感谢 Serge,我编辑了我的文档以显示我的 Python 文档中的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 1970-01-01
  • 2023-01-07
  • 1970-01-01
相关资源
最近更新 更多