+ 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))
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! ☺️
+ 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..
+ 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
+ 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
+ 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.
0
I hope you understand that this code can be reduced to a minimum by writing everything in one line? Do it ☺️
- 1
w = input()
l = input()
print(int(w.count(l)/len(w)*100))
The shortest one liner for this solution, i suggest