+ 2

please explain comments section in 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)))

9th Mar 2018, 1:26 PM
Venkatesh
1 ответ
+ 15
1. loop will run for each character of the alphabet, 'char' will hold the value of each letter one by one. 2. it counts the occurences of each character and divides by total length to get the percentance of character appearance in the file. 3. prints char - perc formatted, it will replace {0} with the first argument you pass (character) and {1} with the second (rounded percentance)
9th Mar 2018, 1:34 PM
Valen.H. ~
Valen.H. ~ - avatar