mardi 15 janvier 2019

How to shorten a long list of if statements using another method, such as for loops

I am building a function in Python, and I have configured something that works, but it is very inefficient.

   if board[0][0] == 1:
    detailed[1][2] = "X"
if board[1][0] == 1:
    detailed[3][2] = "X"
if board[2][0] == 1:
    detailed[5][2] = "X"
if board[0][1] == 1:
    detailed[1][6] = "X"
if board[1][1] == 1:
    detailed[3][6] = "X"
if board[2][1] == 1:
    detailed[5][6] = "X"
if board[0][2] == 1:
    detailed[1][10] = "X"
if board[1][2] == 1:
    detailed[3][10] = "X"
if board[2][2] == 1:
    detailed[5][10] = "X"

I know that I can use for loops to make this look nicer/more efficient, but become stuck almost immediately. So far this is what I have:

for x in range(3):
    for y in range(3):
        if board[y][x] == 1:

This handles the first part of each if statement, but not the detailed[][] = "X" line. There is a visible pattern, but I don't know how to use it to my advantage.

Thanks for the help.

Aucun commentaire:

Enregistrer un commentaire