+ 1
Letter Frequency problem in Python Data course
Hey all! I'm having issues getting my len() function to work in this code. No matter what I do, it returns 0. Any suggestions would be welcome! text = input() letter = input() x = len(text) y = text.count(letter) frequency = int(y/x)*100 print(frequency)
11 Antworten
+ 3
You could try to solve it with multiply y with 100 first and after that divide with x.
+ 3
Michaela Rehm you can see here what I mean :
https://code.sololearn.com/cUzsYWR0MJFJ/?ref=app
+ 3
Michaela Rehm I can't understand it either.
In general would be better if you gives a link to own attempt. It can happen that one overlooks something.
+ 2
Imagine you enter text of 30 characters and it contains 10 characters you are looking for, then you get 10/30≈0.33
int(1/3)=0 ...😎
+ 2
JaScript weirdly, it worked, though. The real problem is that I don't understand why. Dividing y*100 by x shouldn't change the initial value of x, because before I was getting x==0.
+ 2
Michaela Rehm, You made a mistake in testing, in the code you provided, X will never be zero if you enter any text.
+ 1
Solo I don't understand how to get the length of the whole text then, without it taking letter into account
+ 1
Michaela Rehm, Do not be afraid to experiment, play with the code and you will succeed. In programming, the main thing is to learn to think logically, and only then study without a finite number of methods and functions... 😎
+ 1
JaScript I still don't understand how that gets len(text) != 0. When I was having problems with my program, I went through debugging by printing x and y before anything else and my x was ALWAYS == 0.
0
JaScript yeah but my problem is that x keeps returning 0, so no matter what I do, I'm divide by 0, which is not only wrong but impossible.
0
Solo still doesn't help. My logic was sound but was incorrect. x = len(text) SHOULD by all means, give me the length of the text and nothing else. It absolutely shouldn't give me 0, but it does. So not only do I need the correct answer, I need to understand WHY it's correct so that I can understand the logic better for next time.