简体   繁体   English

如何解决“'NoneType'类型的参数不可迭代”的问题?

[英]How to fix this the problem of "argument of type 'NoneType' is not iterable"?

I tried to solve a Python exercise but my program blows up while runing.我试图解决一个 Python 练习,但我的程序在运行时崩溃了。

The exercise instructions are:练习说明如下:

Open the file romeo.txt and read it line by line.打开文件 romeo.txt 并逐行阅读。 For each line, split the line into a list of words using the split() method.对于每一行,使用 split() 方法将该行拆分为单词列表。 The program should build a list of words.该程序应该建立一个单词列表。 For each word on each line check to see if the word is already in the list and if not append it to the list.对于每行上的每个单词,检查该单词是否已经在列表中,如果没有,则将其附加到列表中。 When the program completes, sort and print the resulting words in alphabetical order.程序完成后,按字母顺序对结果单词进行排序和打印。

Here is my code:这是我的代码:

fname = input("Enter file name: ")
fh = open(fname)
j=-1
glolist=list()
for line in fh:
    line=line.rstrip()
    lst=line.split()
    nb=len(lst)
    i=0
    while i< nb:
        if lst[i] not in glolist:    #argument of type 'NoneType' is not iterable"
            i=i+1
        else:
            j=i
            i=i+1
    if j==-1:
        glolist=glolist+lst
    else:
        h=0
        while h<j:
            glolist=lst.append(lst[h])
            h=h+1
        h=h+1
        while h<nb:
            glolist=lst.append(lst[h])
            h=h+1
glolist=glolist.sort()
print(glolist)
fname = input("Enter file name: ")
fh = open(fname)
words=list()
for line in fh:
    line=line.rstrip()
    lst=line.split()
    i=0
    while i<len(lst):
        str=lst[i]
        if str in words:
            i=i+1
        else:
            words.append(str)
            i=i+1
words.sort()
print(words)

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

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