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
8 Respostas
+ 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))
0
Thanks
0
what does
'for key, value in chars.items():' do.
And chars. items is not defined
0
It iterate through a dictionary (chars).
chars = {'a': 1}
key -> 'a'
value -> 1
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
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)
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.
0