當我運行程序時,函數percentagefreq
返回「none」。我的函數沒有返回期望的值
我在做什麼錯?
def main():
file = raw_input("Please enter name of file: ")
s = readfile(file)
print
print "The file contains:"
print s
lc = tolowercase(s)
print "The file in lowercase is:"
print lc
print
compressedTxt = nospaces(lc)
print "The alphabetic characters alone are:"
print compressedTxt
print
Count = alpha(lc)
nonchr = len(s) - Count
print "The number of alphabetic characters are: ", Count
print "The number of non-alphabetic charcters are: ", nonchr
print
print "Letter\tFrequency\tPercentage"
countFreq(compressedTxt)
percent = percentagefreq(Count)
print percent
def readfile(file):
text = open(file,"r")
s = text.read()
text.close()
return s
def tolowercase(s):
lc = ""
for x in s:
if "A" <= x <= "Z":
lc = lc + chr(ord(x) + 32)
else:
lc = lc + x
return lc
def nospaces(lc):
compressedTxt = ""
for x in lc:
if "a" <= x <= "z":
compressedTxt = compressedTxt + x
return compressedTxt
def alpha(lc):
Count = 0
for x in lc:
if "a" <= x <= "z":
Count = Count + 1
return Count
def countFreq(compressedTxt):
global freq
freq = ""
az = ('abcdefghijklmnopqrstuvwxyz')
for x in az:
freq = compressedTxt.count(x)
print x, "\t", freq,"\t"
def percentagefreq(Count):
for i in range(freq):
percent = i/Count
return percent
#I am trying to calculate the percentage of each letter appearing in a file of text
main()
使用'collections.Counter'這些工作 – JBernardo
BTW你的鱈魚e非常和諧...... – JBernardo