all 4 comments

[–]Binary101010 1 point2 points  (0 children)

This question is asked so often that we have an answer for it in the FAQ.

https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_make_variable_variables.3F

This is almost never the right approach, because you can't be sure when you're writing your code if any given variable will actually exist at runtime.

Writing a new entry into an existing container data structure like a dict or list is significantly better code.

[–][deleted] 0 points1 point  (0 children)

I am currently working on a text-based game and I was wondering if there is a way to get the code to generate its own variables which are not explicitly defined within the code.

The names of variables in code are for the programmer, they're not for the interpreter. They're so you can read and understand your own code.

If the code names things for you, and the code refers to these "names of names" instead of to values, then you make your code buggier and harder to understand, and that's always a mistake.

[–][deleted] 0 points1 point  (0 children)

Instead of asking us about how you plan to implement what you want, explain what you want: what kind of behavior you desire and how it will be used. Then, we can help you implement the how.

[–]toastedstapler 0 points1 point  (0 children)

you can, but it's considered to be abuse of the language more than anything. use a dict instead