0

can you help me make this more compact? I need to make it so that for every value of board[0] I get another picture. My script works but I find it too long and bulky.

if board[0] == 0 :
    square_1_button.config(image=T0S)
if board[0] == 1 :
    square_1_button.config(image=T1S)
if board[0] == 2 :
    square_1_button.config(image=T2S)
if board[0] == 3 :
    square_1_button.config(image=T3S)
if board[0] == 4 :
    square_1_button.config(image=T4S)
if board[0] == 5 :
    square_1_button.config(image=T5S)
if board[0] == 6 :
    square_1_button.config(image=T6S)
if board[0] == 7 :
    square_1_button.config(image=T7S)
if board[0] == 8 :
    square_1_button.config(image=T8S)
if board[0] == 9 :
    square_1_button.config(image=T9S)
if board[0] == 10 :
    square_1_button.config(image=T10S)
if board[0] == 11 :
    square_1_button.config(image=T11S)
if board[0] == 12 :
    square_1_button.config(image=T12S)
if board[0] == 13 :
    square_1_button.config(image=T13S)
if board[0] == 14 :
    square_1_button.config(image=T14S)
if board[0] == 15 :
    square_1_button.config(image=T15S)
if board[0] == 16 :
    square_1_button.config(image=T16S)
if board[0] == 17 :
    square_1_button.config(image=T17S)
if board[0] == 18 :
    square_1_button.config(image=T18S)
if board[0] == 19 :
    square_1_button.config(image=T19S)
if board[0] == 20 :
    square_1_button.config(image=T20S)
if board[0] == 21 :
    square_1_button.config(image=T21S)
if board[0] == 22 :
    square_1_button.config(image=T22S)
if board[0] == 23 :
    square_1_button.config(image=T23S)
if board[0] >= 24 :
    square_1_button.config(image=T24S)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Have you tried anything? It looks like you could use a look-up table, from an integer index to an image string. I don't use Python, but would an array be a good place to start? – halfer Jan 06 '18 at 11:37
  • how about using some kind of map / array to capture mapping of the values? – jediz Jan 06 '18 at 11:53
  • I'm voting to close this question as off-topic because this is more a code review question. – Joey Jan 16 '18 at 19:25

2 Answers2

1

Assuming that in T#S is a variable name, you could do this:

if board[0] < 24:
    square_1_button.config(image=eval("T{0}S".format(board[0])))
else:
    square_1_button.config(image=T24S)
x.projekt
  • 507
  • 2
  • 7
  • 20
  • It's rarely a good idea to use `eval`. I think this would be better if the images were in a list, so you could use something like `image[board[0]]`, but otherwise, this is a good solution. – Bryan Oakley Jan 06 '18 at 13:47
  • @BryanOakley - Thanks, but why is it a not a good idea to use eval? – x.projekt Jan 06 '18 at 16:13
  • 1
    Most of the time you don't really need it, it's also a security risk (execution of arbitrary code). See https://stackoverflow.com/questions/1087255/use-of-eval-in-python for some ideas – progmatico Jan 06 '18 at 16:45
  • 1
    @harshatech2012: like progmatico said, you don't need it and it's a security risk (though, not in this case). Plus, it simply makes the code harder to understand. You should optimize for readability. `images[board[0]]` is much easier to grok at a glance than `eval("T{0}S".format(board[0]))`. – Bryan Oakley Jan 06 '18 at 16:56
  • if board[0] < 25: square_1_button.config(image=eval("T{0}S".format(board[0]))) – Omi Harjani Feb 26 '18 at 10:34
0

I suggest using a list:

images = [ T0S,  T1S,  T2S,  T3S,  T4S,  T5S,  T6S,  T7S,  T8S,  T9S,
           T10S, T11S, T12S, T13S, T14S, T15S, T16S, T17S, T18S, T19S,
           T20S, T21S, T22S, T23S, T24S,
]

image_index = board[0] if board[0] <= 24 else 24

square_1_button.config(image=images[image_index])

Where image_index = board[0] if board[0] <= 24 else 24 is equivalent of:

if board[0] <= 24:
    image_index = board[0]
else:
    image_index = 24
akarilimano
  • 1,034
  • 1
  • 10
  • 17