+ 2

Why is the words count always wrong

This is program that takes a text and outputs how many letter / nums etc have been used. But for some reasons it always gets the no. Of words wrong, and I really don't know why as the piece of code written for the words count part is super simple, but I can't seem to spot the error. Here is the code. https://code.sololearn.com/cDWvpNQ7N73K/?ref=app

27th Nov 2023, 6:52 AM
Intermediate Depression
Intermediate Depression - avatar
11 Respuestas
+ 5
Intermediate Depression you used the variable 'words' two times in different context. The first one on the top part is correct and is the one you want, but inside your code(line 22), you used words again to do something different. That words had a last value of 1, which was finally printed. Suggest you rename the duplicated variable name to something else.
27th Nov 2023, 8:54 AM
Bob_Li
Bob_Li - avatar
+ 2
The declaration and use was so far apart, it's inevitable.😁
28th Nov 2023, 9:06 AM
Bob_Li
Bob_Li - avatar
+ 2
Constants are usually declared at the beginning. But yes, I suppose declaring variables near the point of use would make it easier to keep track if them. I think I also saw a similar recommendation for c++.. https://google.github.io/styleguide/cppguide.html#Local_Variables
28th Nov 2023, 10:32 AM
Bob_Li
Bob_Li - avatar
+ 2
Wong Hei Ming yes, anything that organizes|simplify the data | process is always helpful in any coding situation. But these insights are gained after the messy exploratory phase. Premature optimization should be avoided in prototyping.
28th Nov 2023, 11:05 AM
Bob_Li
Bob_Li - avatar
+ 2
Intermediate Depression , Your code is hard to read. I recommend inserting blank lines to visually separate chunks of code that do different things. I agree with the suggestion to declare variables where they are used. That would help the reader understand what they are for.
28th Nov 2023, 7:03 PM
Rain
Rain - avatar
+ 1
Thank you so much, I didn't notice 😅.
28th Nov 2023, 9:02 AM
Intermediate Depression
Intermediate Depression - avatar
+ 1
Bob_Li Side question. I saw Java recommend to declare a variable when it is needed, instead of declare all of them in the beginning. Does it apply to other languages too?
28th Nov 2023, 9:50 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Bob_Li Personally I think using a dict can simplify the code a lot, and optionally using string module helps too.
28th Nov 2023, 10:58 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Rain , Yes i am aware, but I wouldn't change a single line of code in this program although now I can see so many flaws. This was my very first program ever! I wrote it after 2 weeks into learning python. I just decided to add one more feature to it all of a sudden. Now I will just frame it and put it somewhere on the wall 😂😂😂 I feel so proud I could write such program after only 2 weeks😂.
29th Nov 2023, 4:21 AM
Intermediate Depression
Intermediate Depression - avatar
0
Intermediate Depression It is amazing you can write a code like this only learning for 2 weeks. Good Job!
29th Nov 2023, 4:24 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Wong Hei Ming Thank you so much ! I improved so much now!!!
29th Nov 2023, 6:03 PM
Intermediate Depression
Intermediate Depression - avatar