I can't understand this code.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

I can't understand this code..

def count_char(text, char): count = 0 for c in text: if c == char: count += 1 return count filename = input("Enter a filename: ") with open(filename) as f: text = f.read() for char in "abcdefghijklmnopqrstuvwxyz": perc = 100 * count_char(text, char) / len(text) print("{0} - {1}%".format(char, round(perc, 2)))

11th Jul 2020, 3:32 AM
🅿︎Ʀꍏ🄽🅰︎վ ℕᗩ𝕴🅳ꀎ
🅿︎Ʀꍏ🄽🅰︎վ ℕᗩ𝕴🅳ꀎ - avatar
2 Answers
+ 3
Overview: The program prints the percentage of frequency for each letter of the alphabet in a user-specified file. The sequence is basically: 1. Prompt user for a file name. 2. Print frequency percentage for each letter of the alphabet. count_char is named very clearly. It counts the number of occurrences of the specified character in the string called text. count_char is used as part of the frequency percentage calculation. Let me know if you have a more specific question about a specific part of the program.
11th Jul 2020, 4:23 AM
Josh Greig
Josh Greig - avatar
11th Jul 2020, 4:18 AM
Bilbo Baggins
Bilbo Baggins - avatar