for example, if i have a list like:
one = [1,2,3]
what function or method can i use to split each element into their own separate list like:
one = [1]
RANDOM_DYNAMIC_NAME = [2]
RANDOM_DYNAMIC_NAME_AGAIN = [3]
and at any given time, the unsplit list called one may have more than 1 element, its dynamic, and this algorithm is needed for a hangman game i am coding as self-given homework.
the algorithm is needed to complete this example purpose:
pick a word: mississippi
guess a letter: s
['_','_','s','s','_','s','s','_','_','_','_']
Here is my code:
Looking at your code, if the part you’re trying to solve is the comments in lines 24-26, you definitely don’t need dynamically-created variables for that at all, and in fact I can’t even imagine how they could help you.
You’ve got this:
The names of your variables are very confusing—something called
wordis the guessed letter, while you’ve got a different variableletterguessthat’s something else, and then a variable calledletterthat’s the whole word… But I think I get what you’re aiming for.enumis a list of all of the indices ofwordwithinletterlist. For example, ifletterlistis'letter'andwordist, it will be[2, 3].Then you do this:
So now
bracketstripis['2', '3']. I’m not sure why you want that.And
''.join(bracketstrip)is'23', sozis23.And now you get an
IndexError, because you’re trying to setletterguess[23]instead of settingletterguess[2]andletterguess[3].Here’s what I think you want to replace that with:
A few hints about some other parts of your code:
You’ve got a few places where you do things like this:
This is the same as
letterlist = list(letter). But really, you don’t need that list at all. The only thing you do with that isfor i, x in enumerate(letterlist), and you could have done the exact same thing withletterin the first place. You’re generally making things much harder for yourself than you have to. Make sure you actually understand why you’ve written each line of code.“Because I couldn’t get it to work any other way” isn’t a reason—what were you trying to get to work? Why did you think you needed a
listof letters? Nobody can keep all of those decisions in their head at once. The more skill you have, the more of your code will be so obvious to you that it doesn’t need comments, but you’ll never get to the point where you don’t need any. When you’re just starting out, every time you figure out how to do something, add a comment reminding yourself what you were trying to do, and why it works. You can always remove comments later; you can never get back comments that you didn’t write.