+ 1
Text analyzer
I'm having trouble understanding this entire text analyzer section. Can Someone explain this code? -boivbhf jnl gb qb vg. Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu. Abj vf orggre guna arrire. Nygubhtu arire vf bsgra orggre guna *evtug* abj. Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn. Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn. Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!""") 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 Answer
+ 2
It isnt too complicated.
The file is just jibberish. Nothing important there.
so first off we see that newfile.txt is opened and is called f. then text = all of the text in that file (all the jibberish). Now we can perform functions on text, and get information on its content. (pretty basic file reading stuff, but definitely can be confusing at first).
starting with the for loop.
the code goes through each letter in the english alphabet. for each of those letters it gets the number of times it appears in the text: text.count (letter). it then multiplies that number by 100 (to make it a percentage, rather than a decimal) and finally divides it by the total number of characters in the text.
once this calculation is done, it prints out the info and then the for loop moves on to the next letter and repeats the process.