繁体   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()

为什么这段代码不起作用? 我尝试在代码的其他 forms 中使用关键字 Global 并且我尝试了多种代码调整,但似乎无法正确处理。

这是在 function 中调用 function 的正确形式吗?

终端提示:

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

另一种解决方案是在代码中添加两行:添加到:count_char():

global count

和 count_sentences():

global sentence

注意:在这种情况下,您不需要从函数中“返回”任何东西。

暂无
暂无

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

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