简体   繁体   English

为什么我的代码说名称“计数”未定义

[英]why does my code say name 'count' is not defined

words = input("Text: ")

def l_creator():
    count_char()
    count_sentences()
    print((count / sentence) * 100)

def count_char():
    count = 0
    for i in words:
        if i != ' ':
            count += 1  #number of words
    return count

def count_sentences():
    for i in words:
        a = words.split(' ') #number of sentences
    sentence = len(a) * 100
    return sentence


l_creator()

Why doesn't this code work?为什么这段代码不起作用? I've tried to use the keyword Global in other forms of the code and I've tried multiple accommodations of the code but just can't seem to get it right.我尝试在代码的其他 forms 中使用关键字 Global 并且我尝试了多种代码调整,但似乎无法正确处理。

Is this the right form to call a function inside a function?这是在 function 中调用 function 的正确形式吗?

The terminal prompts:终端提示:

File "read.py", line 32, in <module>
    l_creator()
  File "read.py", line 14, in l_creator
    count_char(count)
NameError: name 'count' is not defined

An other solution is to add two lines in you code: add to: count_char():另一种解决方案是在代码中添加两行:添加到:count_char():

global count

and to count_sentences():和 count_sentences():

global sentence

Nb: in this case you don't need to "return " any thing from functions.注意:在这种情况下,您不需要从函数中“返回”任何东西。

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

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