+ 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)

5th Jul 2023, 6:36 PM
Michaela Rehm
Michaela Rehm - avatar
11 Respostas
+ 3
You could try to solve it with multiply y with 100 first and after that divide with x.
5th Jul 2023, 8:32 PM
JaScript
JaScript - avatar
5th Jul 2023, 8:50 PM
JaScript
JaScript - avatar
+ 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.
6th Jul 2023, 5:07 PM
JaScript
JaScript - avatar
+ 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 ...😎
5th Jul 2023, 7:32 PM
Solo
Solo - avatar
+ 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.
6th Jul 2023, 2:08 PM
Michaela Rehm
Michaela Rehm - avatar
+ 2
Michaela Rehm, You made a mistake in testing, in the code you provided, X will never be zero if you enter any text.
7th Jul 2023, 11:37 PM
Solo
Solo - avatar
+ 1
Solo I don't understand how to get the length of the whole text then, without it taking letter into account
5th Jul 2023, 7:41 PM
Michaela Rehm
Michaela Rehm - avatar
+ 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... 😎
6th Jul 2023, 7:38 AM
Solo
Solo - avatar
+ 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.
6th Jul 2023, 2:05 PM
Michaela Rehm
Michaela Rehm - avatar
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.
5th Jul 2023, 8:34 PM
Michaela Rehm
Michaela Rehm - avatar
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.
6th Jul 2023, 2:00 PM
Michaela Rehm
Michaela Rehm - avatar