1

Here again with a proposal and a need;
as you can know from my previuos question i'm building a simple slot machine script.
I want five numbers to be printed in a minimal board, the board is 9x9 '[]'

like this:

[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []

numbers will be printed in center of the grid like this:

[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] O O O O O [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []

but what i want, like everyone building a board like this one,array is a fixed width string with numbers inside it.

the function that print out the entire board is this one:

slot = []
for row in range(9):
    slot.append(['[]'] * 9)

def print_slot(slot):
    for element in slot:
        print(" ".join(element))
print_slot(slot)

how can i use a format tool like .format if in the second part of my script i will need to acces the list slot at the exact index of every '[]' to do a substitution with numbers?

def game_start():
    if start == str(' '):
        first_n = random.randint(0, 9)
        second_n = random.randint(0, 9)
        third_n = random.randint(0, 9)
        fourth_n = random.randint(0, 9)
        fifth_n = random.randint(0, 9)
        slot[4][2] = str(first_n)
        slot[4][3] = str(second_n)
        slot[4][4] = str(third_n)
        slot[4][5] = str(fourth_n)
        slot[4][6] = str(fifth_n)
        print_slot(slot)
game_start()

it's time to build the array for the ultimate fixed width board for everyone in need of it!

:-)
Mat
  • 35
  • 5

1 Answers1

1

You can use padding with string formatting to accomplish this pretty easily

First: in your example, you named the rows element but for sanity's sake, it should be

def print_slot(slot):
    for row in slot:
        print(" ".join(row))

with the new name, you can do

def print_slot(slot):
    for row in slot:
        print(" ".join('{: >2}'.format(element) for element in row))

to pad the left side so that it has a width of 2

[] [] [] [] [] [] [] [] []                                                                                                                                  
[] [] [] [] [] [] [] [] []                                                                                                                                  
[] [] [] [] [] [] [] [] []                                                                                                                                  
[] [] [] [] [] [] [] [] []                                                                                                                                  
[] []  1  6  8  5  6 [] []                                                                                                                                  
[] [] [] [] [] [] [] [] []                                                                                                                                  
[] [] [] [] [] [] [] [] []                                                                                                                                  
[] [] [] [] [] [] [] [] []                                                                                                                                  
[] [] [] [] [] [] [] [] [] 

I would do it like this, personally:

slot = []
for row in range(9):
    slot.append([None] * 9)

def print_slot(slot, empty='[ ]'):
    for row in slot:
        print(' '.join(empty if element is None else '{{: ^{}}}'.format(len(empty)).format(element) for element in row))

which will center the elements, making it look like this

[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ]  7   7   9   8   4  [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
Ngenator
  • 10,909
  • 4
  • 41
  • 46
  • wow, this completely answer my question, but i'm not able to fully understand how it works, i really have to study a lot! – Mat Dec 24 '14 at 17:57
  • @Mat is there something specific that you dont understand? The first example is definitely simpler since it formats all of the elements including the ones that are `[]` The second example is more advanced and probably not good for homework – Ngenator Dec 24 '14 at 18:10
  • i don't get what exatly do the line `(" ".join('{: >2}'.format(element) for element in row))` and i'm not still able to define a function that erase the board e write it again at every step of the program like i asked [here](http://stackoverflow.com/questions/27637325/how-can-avoid-the-output-to-look-like-a-logpython) today – Mat Dec 24 '14 at 18:18
  • @Mat basically, for every `element` in `row` format the element using `string.format` to add spaces to the left until the resulting string is 2 characters long. The link in the answer is to the docs for `string.format`. – Ngenator Dec 24 '14 at 18:24