这是我的代码,可从单词列表中找到复杂的单词。
“ complex_words.py”
def complex_words_count(words_list):
cw_count = 0
vowels = ['a', 'e', 'i', 'o', 'u']
for word in words_list:
syl_count = 0
for w in vowels:
if w in word and word[-2:len(word)] != 'es | ed':
syl_count+=1
if syl_count > 2:
cw_count+=1
return cw_count
from Words_Count import word_count
def complex_words_percent():
cwc = complex_words_count(words_list)
wc = word_count(words_list)
percent_cw = int(cwc/wc*100)
return percent_cw
“ Words_Count.py”
def word_count(words_list):
wc = len(words_list)
return wc
“ Test.py”
from complex_words import complex_words_count, complex_words_percent
a = ['inferno', 'shamed', 'changes', 'umbrella']
cw_c = complex_words_count(a)
cw_p = complex_words_percent()
complex_words_count()函数绝对可以正常工作,但问题出在complex_words_percent()
Here's a screenshot representing the fault in the function
运行“ Test.py”时的输出:
我没有办法正确运行“ Test.py”。
答案 0 :(得分:2)
您将单词列表a
传递给了complex_words_count
函数,但是complex_words_percent
对此一无所知。您还必须将其作为该函数的参数添加:
def complex_words_percent(words_list):
cwc = complex_words_count(words_list)
wc = word_count(words_list)
percent_cw = int(cwc/wc*100)
return percent_cw
并在调用函数时将其传递:
cw_p = complex_words_percent(a)
如果要在函数之间共享值,则应将它们放在一个类中:
class ComplexWords:
def __init__(self, words_list):
self.words_list = words_list
def count(self):
words_list = self.words_list
... rest of code here ...
def percent(self):
words_list = self.words_list
... rest of code here ...
然后使用它:
cw = ComplexWords(a)
cw_c = cw.count()
cw_p = cw.percent()
答案 1 :(得分:0)
编辑为“ complex_words.py”
w0 = []
def complex_words_count(words_list):
global w0
w0 = words_list
cw_count = 0
vowels = ['a', 'e', 'i', 'o', 'u']
for word in words_list:
syl_count = 0
for w in vowels:
if w in word and word[-2:len(word)] != 'es | ed':
syl_count+=1
if syl_count > 2:
cw_count+=1
return cw_count
from Words_Count import word_count
def complex_words_percent(words_list = None):
if words_list is None:
words_list = w0
cwc = complex_words_count(w0)
wc = word_count(w0)
percent_cw = int(cwc/wc*100)
return percent_cw
这给了我想要的“ Test.py”的确切输出