+ 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
11 odpowiedzi
+ 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.
+ 2
The declaration and use was so far apart, it's inevitable.😁
+ 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
+ 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.
+ 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.
+ 1
Thank you so much, I didn't notice 😅.
+ 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?
+ 1
Bob_Li
Personally I think using a dict can simplify the code a lot, and optionally using string module helps too.
+ 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😂.
0
Intermediate Depression
It is amazing you can write a code like this only learning for 2 weeks.
Good Job!
0
Wong Hei Ming
Thank you so much ! I improved so much now!!!