我需要製作一個程序來讀取文本文件並打印出有多少元音和輔音。我製作一個文本文件進行測試,其中唯一的內容是「這是一個測試」。然而總是輸出它:計算文件中的元音和輔音(Python)
輸入要檢查的文件:test.txt的
元音的數量爲:1
輔音的數量:0
fileName = input("Enter the file to check: ").strip()
infile = open(fileName, "r")
vowels = set("A E I O U a e i o u")
cons = set("b c d f g h j k l m n p q r s t v w x y z B C D F G H J K L M N P Q R S T V W X Y Z")
text = infile.read().split()
countV = 0
for V in text:
if V in vowels:
countV += 1
countC = 0
for C in text:
if C in cons:
countC += 1
print("The number of Vowels is: ",countV,"\nThe number of consonants is: ",countC)
如果有是一個更好的方式來輸入元音和缺點的值我也想知道,因爲當我嘗試用戶.lower()將文件中的所有內容轉換爲小寫時,出現錯誤.....
因爲你計算空間以及? –