我必须编写一个函数 count_words(),它接受一个字符串列表并以 int 的形式返回该列表中不同单词的 int 数量。清单如下:
List = ['twas', 'brillig', 'and', 'the', 'slithy', 'toves', 'did', 'gyre',
'and', 'gimble', 'in', 'the', 'wabe', 'all', 'mimsy']
我已经尝试使用以下代码进行操作:
def count_words(url): #this is the first line of the code but it was not included with the lines below for some reason.
mylist = function(url) #the function function(url) reads through the url and returns all words from the website in a list of strings.
counts = 0
for i in mylist:
if i not in mylist:
counts = counts + 1
else:
continue
return counts
从这里我不知道该怎么办。我收到“for i in mylist”行的错误,我不知道如何解决。我仍然是初学者,所以非常基本的答案就可以了。我不介意我是否必须更改整个代码。我唯一不能改变的是 'mylist = function(url)' 行,因为该部分有效,我们必须包含它。
我回来的错误是:
Traceback (most recent call last):
File "<web session>", line 1, in <module>
File "/home/karanyos/foc/proj1-karanyos/karanyos.py", line 24, in count_words
for i in mylist:
TypeError: 'NoneType' object is not iterable
提前致谢,
基利