简体   繁体   中英

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.

Is this the right form to call a function inside a 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():

global count

and to count_sentences():

global sentence

Nb: in this case you don't need to "return " any thing from functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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