+ 3

Python letter frequency project

I wrote a code to solve the letter frequency project ( python data structure project 1 ) And got the 1,4,5 cases right and the rest have wrong answers so i need someone to tell me whats the problem or what went wrong in my program #your code goes here text=input() letter=input() count=0 freq=0 for letter in text: count=text.count(letter) x=(count/len(text)) freq= x*100 print (int(freq))

9th Feb 2022, 11:47 AM
Mirna Abu-Dhaim
Mirna Abu-Dhaim - avatar
8 Réponses
+ 4
You use the count() method, it has already calculated everything for you: #your code goes here text=input() letter=input() count=text.count(letter) print(count) If you do not understand this, then you have not mastered the material covered and you definitely need to repeat it from the very beginning. Happy learning! ☺️
9th Feb 2022, 1:42 PM
Solo
Solo - avatar
+ 3
No need of loop there. Remove it. Loop making count last letter only but you need to count for input letter.. edit: post full question if not solved..
9th Feb 2022, 11:56 AM
Jayakrishna 🇮🇳
+ 2
Ohh thanks i got it ,my mistake was For >> counts every letter If >> count a specific letter So i should use if not for
9th Feb 2022, 11:58 AM
Mirna Abu-Dhaim
Mirna Abu-Dhaim - avatar
+ 1
for i in text: if i == letter: count += 1 sets variable count to how many times variable letter is in variable text while your code for letter in text: count=text.count(letter) count is set to how many times the last letter appears in variable text ex. missisipi count is set to 4 as the last letter is "i" and it appears 4 times so while for loop can be used, you used it wrong, i would recommend to re-read the course again to hopefully get better at it
10th Feb 2022, 7:44 PM
fajnah6
+ 1
text = input() search_letter = input() count = 0 freq = 0 for letter in text: if letter == search_letter: count += 1 x = count / len(text) freq = x * 100 print(int(freq)) This took me a while while trying different things, but here's what I got for the Letter Frequency project in Python.
13th Mar 2023, 4:17 PM
Joshua Nichols
Joshua Nichols - avatar
0
I hope you understand that this code can be reduced to a minimum by writing everything in one line? Do it ☺️
9th Feb 2022, 2:13 PM
Solo
Solo - avatar
- 1
w = input() l = input() print(int(w.count(l)/len(w)*100)) The shortest one liner for this solution, i suggest
5th Sep 2022, 9:41 PM
Willem van Dorland
Willem van Dorland - avatar