[英]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.