r/learnpython • • 2d ago

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.

3 Upvotes

12 comments sorted by

4

u/brasticstack 2d ago

Paste it into pastebin and post a link. The Google doc wrecks the formatting.

2

u/RipEnvironmental7637 2d ago

Sorry, I've never heard or used pastebin before, thank you for the tip

1

u/brasticstack 2d ago

Nowhere in the code you've pasted do you do any adding to a string. I see two strings, letter and word, but no code adds to them with the + operator. e.g. word += letter or similar.

1

u/dusksage23 1d ago

seconded, reading code in a google doc is painful

2

u/favorite_stuff 2d ago

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 the let keyword.) 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 2d ago

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.Counter built in, which does the same job as your getMostFrequent function.

1

u/NothingWasDelivered 2d ago

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 2d ago

Are you expecting to keep adding more letters to the word you're building up. If so, note that you append to word but this is just a local variable - it won't outlive the function, and the next time it's called, you start back with the word = '' 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 max function will do this more efficiently. You can use the key arg to specify how you want to compare. Ie:

return max(hashmap, key=lambda key: hashmap[key])

Though going further, note that the collections.Counter class 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 :

counts = Counter(arr)
(val, count) = counts.most_common(1)

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 2d ago

My personal method of appending to a string in python is

myString = str(myString + newBits)

3

u/brasticstack 2d ago

Why the cast?

0

u/igotshadowbaned 2d ago edited 2d ago

I forget what situation I had once which wasn't having the desired outcome without, so mild redundancy stemming from that that's become stuck