-1

I am trying to make my variables "flexible" using a for loop, but I can't get it to work.

I want it to ouput the messages I wrote, I tried 'message_' without the '' but that just gives an error that it's not an actual variable.

Thanks in advance for the help.

Here is my code snippit:

message_1 = 'This is message 1.'
message_2 = 'This is message 2.'
message_3 = 'This is message 3.'
message_4 = 'This is message 4.'

for x in range(1, 5):
    print('message_' + str(x))

Output:

Message_1

Message_2

Message_3

Message_4

Niels
  • 9
  • 2

1 Answers1

0

A better practice is to store everything inside a dictionary.

data = {
  'message_1' : 'This is message 1.',
  'message_2' : 'This is message 2.',
  'message_3' : 'This is message 3.',
  'message_4' : 'This is message 4.'
}

data['message_' + str(x)]

Dynamic dictionary assignment is also possible

data = { f'message_{i}' : f'This is message {i}.' for i in range(1,5) }

If you must access local variables, the locals() function returns a dictionary of all local variables but is not recommended for something like this.

fibonachoceres
  • 727
  • 4
  • 14
  • So what I'm trying to do is not a thing in Python? Because I remember it being a thing in PHP or C#. – Niels Dec 20 '21 at 10:39
  • It's definitely doable but accessing local state like this is not recommended. You can always use tricks and hacks to pull it off but there's almost a better way. If you can elaborate your use case of why you need to metaprogram it like this maybe it would make more sense, but if you just want a key-value mapping dictionaries are the "Pythonic" way to do it. Also just to be sure you're not referring to variable accessing an object's state because that is a thing with getattr() – fibonachoceres Dec 20 '21 at 10:44
  • My project is a board game, and I have 4 pawns. But It's not finished I'm trying to make it so the game has turns. But at the moment my code is designed for 1 pawn and I don't feel like copying everything 4 times, because that's sloppy. So I was hoping to use a for loop with flexible variables, so that I have to write less if statements when for example checking if the pawn landed on a red spot. I hope this kinda gives you an idea of what i'm trying to do, it's hard to describe while having a 500 character limit. – Niels Dec 20 '21 at 10:58
  • Or checking which pawn is selected. – Niels Dec 20 '21 at 11:01
  • 1
    I understand what you mean, but you can use a loop to generate dictionary key names and do the same thing as well while looking up. Your example doesn't seem to demand that each pawn be a variable. If you were designing classes to represent a Board and Pieces then that would be a slightly different scenario, but again without the need for special functionality a dictionary is still the best native structure for containing everything. You would still have to flesh out the starting portion of each individual pawn explicitly in any case unless you can devise a pattern to generate. – fibonachoceres Dec 20 '21 at 11:26
  • I've modified my answer in case you aren't familiar with looping and creating dictionaries. – fibonachoceres Dec 20 '21 at 11:28
  • Ah I will try that, thank you. – Niels Dec 20 '21 at 11:39