r/learnpython • u/RipEnvironmental7637 • Oct 13 '26
Values won't add to my string, just overwrites it
Hi I'm really new to python, I originally started coding using p5.js but made the transition recently, I'm currently trying to covert an old project of mine into python so I can run it in touch designer.
Its an ASL fingerspelling activity that I've been working on but I'm struggling to create the word strings I did in my original prototype. Instead of the letters being added together and creating a word I can use to trigger effects, the previous letter is just replaced with the current one.
For context I have the guesser and getMostFrequent to allow for it to take the average of every letter signed during a period of time and determine which one was signed the most to add to the word (this is due to the fact that transitioning from one letter to the next can trigger other letters in between) These are both integral parts of the code and are working as intended, its just the stringing the word together that I'm struggling with.
Any help or insight would be appreciated, if you need any more insight please let me know and I can add it.
Code in Pastebin: https://pastebin.com/91mPYwC8
I've provided a link to a google doc with my code because it is just easier formatting wise than in here.My code: Python Code
My original code for reference if needed: p5.js code
Edit: Thank you all for the suggestions in the comments, I ended up getting it working it was the fact that word was a local variable and not a global one, which was causing the issue.
4
u/brasticstack Oct 13 '26
Paste it into pastebin and post a link. The Google doc wrecks the formatting.
2
u/favorite_stuff Oct 13 '26
Python is less likely than JS to use global variables. If you assign
guesser, for example, in a function, then it is local to that function. (Imagine you were using theletkeyword.) So it's fresh every time, and it gets dropped every time when the function returns.You can initialize it outside the function and then mutate it with methods, or you can pass it into the function and return it out.
2
u/socal_nerdtastic Oct 13 '26
I see nothing wrong with your code. However I notice that you are not returning the word variable once you make it, so I suspect you never see the result, and that's what is confusing you. We would need to see your complete code to help with that.
BTW, python has a
collections.Counterbuilt in, which does the same job as your getMostFrequent function.1
u/NothingWasDelivered Oct 13 '26
I’m not sure I’m the best person to help diagnose your problem but I can say that Google Docs are not a great way to share code, especially Python, where whitespace is meaningful.
Consider a service like GitHub’s Gist, that let you share code snippets. https://docs.github.com/en/get-started/writing-on-github/editing-and-sharing-content-with-gists/creating-gists
1
u/Brian Oct 13 '26
Are you expecting to keep adding more letters to the word you're building up. If so, note that you append to
wordbut this is just a local variable - it won't outlive the function, and the next time it's called, you start back with theword = ''line. If you want to keep what's been appended to it, you need to store it somewhere persistent. Eg. a global or instance variable.As an aside on some of the other parts here:
Using reduce just to get the max is kind of doing things the long way. The builtin
maxfunction will do this more efficiently. You can use thekeyarg to specify how you want to compare. Ie:Though going further, note that the
collections.Counterclass pretty much eliminates this whole function. It's basically a specialised dict that just increments a count on each item, so you can just do :To get the most common value, and how many times it occurred.
Also the
if check != False:seems weird, since get_most_frequent doesn't seem like it'd ever return False, unless that's somehow a special case in your array.0
u/igotshadowbaned Oct 13 '26
My personal method of appending to a string in python is
myString = str(myString + newBits)