0
Can anyone explain me this code i don't understand it, This code is from lesson
def count_char(text, char): count = 0 for c in text: if c == char: count += 1 return count file = open("newfile.txt", "w") file.write("""Ornhgvshy vf orggre guna htyl. Rkcgunyraprq. guna qrafr. Ernqnovyvgl pbhagf. Fcrpvny pnfrf ner") file.close() filename = "newfile.txt" 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)))
1 ответ
+ 1
There are 3 errors (missing triple quote, indentation of last line, count() instead of count).
Corrected version:
https://code.sololearn.com/c9Wl8N4F4Yuy/?ref=app"
The program writes some dirty stuff on a file. Then it reopens the file and brings its contents in memory ('text'). Finally, for each character in the alphabet starting from 'a', it counts how many times the character is present in 'text' (function count_char()) and calculates/prints its percentual.