我想正確計算文本文件中的元音和輔音的數量,但目前我迷了路。我有其他需要查找的部分。如何計算文本文件中元音和輔音的數量?
# Home work 4
from string import punctuation
fname = raw_input("Enter name of the file: ")
fvar = open(fname, "r")
punctuationList = "!#$%&'(),.:;?"
numLines = 0
numWords = 0
numChars = 0
numPunc = 0
numVowl = 0
numCons = 0
if line in "aeiou":
numVowl = + 1
else:
numCons += 1
for line in fvar:
wordsList = line.split()
numLines += 1
numWords += len(wordsList)
numChars += len(line)
for punctuation in punctuationList:
numPunc += 1
print "Lines %d" % numLines
print "Words %d" % numWords
print "The amount of charcters is %d" % numChars
print "The amount of punctuation is %d" % numPunc
print "The amount of vowls is %d" % numVowl
print "The amount of consonants is %d" % numCons
究竟是什麼問題? – Mureinik
作爲第一步,嘗試編寫一個函數,給定一個**字符串**,返回輔音字母和元音字母的數量。然後從那裏工作。例如,'count(「hello world」)''可能會返回'(7,3)' – sam
您在fvar'循環中的'for line'外有'if line'語句。這應該如何工作? – Barmar