首页 > 解决方案 > I am trying to run following lines of codes

问题描述

Can someone help me with what is wrong with this code?

fname=input("Enter the name of the file:")
fhandle=open(fname)
word=list()
for line in fhandle:  #All lines starting with "From " are split into list of words
    if not line.startswith("From "):
        continue
        words=line.split()
        word=words[1]            #Second word is obtained
 #Dictionary to count occurence
 counts=dict()
 for name in word:
    counts[name] = counts.get(name,0)+1
     print(counts[name],name)
     print(counts)

#Deciding the maximum occurence of name
maxcount=None
maxword=None
for word,count in counts.items():
    if maxcount is None or count > maxcount:  #if we are ont first word or if 
 the current count is greater than previous maximum count
        maxcount=count
        maxword=word
 print(maxword,maxcount)

The output I expected was to print OUTPUT: cwen@iupui.edu 5 but i end up getting None None as output. Can anybody help me cause I'm stuck from past 4 hrs!

标签: python-3.x

解决方案


推荐阅读