如何修复类型错误:元音()缺少所需的位置参数:"文件句柄"?



编写一个程序,将文本文件的内容读取到字符串中

def vowels(filehandle):
num_vowel = 0
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
for c in filehandle:
if c in vowels:
num_vowel = num_vowel+1
return num_vowel
def consonants(filehandle):
num_con = 0
for c in filehandle:
if c >= 'a' and c <= 'z':
if c not in vowels:
num_con = num_con+1
elif c>='A' and c<='Z':
if c not in vowels:
num_con = num_con+1
return num_con
def case(filehandle):
uppercase = 0
lowercase = 0
for c in filehandle:
if c>='a' and c<='z':
uppercase=uppercase+1
elif c>='A' and c<= 'Z':
lowercase=lowecase+1
return uppercase, lowercase
def main():
vowel = vowels()
consonants = consonants()
uppercase, lowercase = case()
try:
filename=input('Enter name of text file: ')
filehandle=open('words.txt', 'r')
print('Vowels:', vowels)
print('Consonants:', consonants)
print('Uppercase:', uppercase)
print('Lowercase:', lowercase)
filehandle.close()
except IOError:
print('FILE NOT FOUND')

main()

当我试图运行程序时,它给了我这个错误

line 31, in main
vowel = vowels()
TypeError: vowels() missing 1 required positional argument: 'filehandle'

我对python和使用函数还是个新手,所以如果你能把它放在任何人都能理解的地方,将不胜感激

vowels()的定义指出它需要一个参数:

def vowels(filehandle):

但在main()中,您调用它时没有参数:

vowel = vowels()

最新更新