0

How do I calculate the percentage of characters in a text based on the users input in python code please. BTW noob here

Character frequency

19th Aug 2017, 5:36 PM
Ade Omotayo
Ade Omotayo - avatar
8 Answers
+ 1
chars = {} for char in text: try: chars[char]+=1 except: chars[char]=1 for key, value in chars.items(): print("{} -> {}%".format(key, value/len(text)*100))
19th Aug 2017, 10:13 PM
clement
clement - avatar
0
Thanks
19th Aug 2017, 10:13 PM
Ade Omotayo
Ade Omotayo - avatar
0
what does 'for key, value in chars.items():' do. And chars. items is not defined
19th Aug 2017, 10:23 PM
Ade Omotayo
Ade Omotayo - avatar
0
It iterate through a dictionary (chars). chars = {'a': 1} key -> 'a' value -> 1
19th Aug 2017, 10:29 PM
clement
clement - avatar
0
Doesn't that mean it simply displays the percentage of every character? I want to be able to input the character and get an output of its percentage
19th Aug 2017, 10:36 PM
Ade Omotayo
Ade Omotayo - avatar
0
Yes it prints every character's pourcentage. If you want to print only the input : try: print(chars[input()]/len(text)*100) except: print(0)
19th Aug 2017, 10:44 PM
clement
clement - avatar
0
You are making a program to analyze text. Take the text as the first input and a letter as the second input, and output the frequency of that letter in the text as a whole percentage. Sample Input: hello l Sample Output: 40 Explanation : The letter l appears 2 times in the text hello, which has 5 letters. So, the frequency would be (2/5)*100 = 40.
12th Jun 2022, 1:23 PM
Élie RUGAGI ISMAEL
Élie RUGAGI ISMAEL - avatar
0
12th Jun 2022, 1:24 PM
Élie RUGAGI ISMAEL
Élie RUGAGI ISMAEL - avatar