0

I'm trying to make a board that looks like this:

x ! x ! x

~ ~ ~ ~ ~

x ! x ! x

~ ~ ~ ~ ~

x ! x ! x

This is what I have so far:

def print_tic_tac_toe(horiz_char, vert_char):
    print(vert_char*2)
    print(horiz_char*5)
    return

print_tic_tac_toe('~', '!')

I can't figure out how to get the 'x' to print between the vertical characters. How should I go about this? Is a newline character involved?

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Avery
  • 3
  • 3
  • Do you would like to do something like this? [square similar](https://stackoverflow.com/questions/36903763/combine-same-function-with-different-parameters-python) – Milor123 May 01 '16 at 15:46

3 Answers3

0

Use join to use the vert_char to join a list of xs. Sth. like so:

def print_tic_tac_toe(horiz_char, vert_char):
    print(vert_char.join(['x']*3))
    print(horiz_char*5)
    print(vert_char.join(['x']*3))
    print(horiz_char*5)
    print(vert_char.join(['x']*3))

print_tic_tac_toe('~', '!')

x!x!x
~~~~~
x!x!x
~~~~~
x!x!x

or shorter:

def print_tic_tac_toe(horiz_char, vert_char):
    print(('\n'+(horiz_char*5)+'\n').join(vert_char.join(['x']*3) for _ in range(3)))
user2390182
  • 72,016
  • 6
  • 67
  • 89
0

You can try like this:

def print_tic_tac_toe(horiz_char = '', vert_char = ''):
    print(('X '+vert_char)*2+'X')
    if horiz_char != '':
        print(horiz_char*5)
    return

print_tic_tac_toe('~ ', '! ')
print_tic_tac_toe('~ ', '! ')
print_tic_tac_toe('', '! ')

Output:

X ! X ! X
~ ~ ~ ~ ~ 
X ! X ! X
~ ~ ~ ~ ~ 
X ! X ! X
Md Mahfuzur Rahman
  • 2,319
  • 2
  • 18
  • 28
0
def print_tic_tac_toe(horiz_char, vert_char):
    print('x %s x %s x' %(vert_char, vert_char))
    print('%s %s %s %s %s' %(horiz_char, horiz_char, horiz_char, horiz_char, horiz_char))
    print('x %s x %s x' %(vert_char, vert_char))
    print('%s %s %s %s %s' %(horiz_char, horiz_char, horiz_char, horiz_char, horiz_char))
    print('x %s x %s x' %(vert_char, vert_char))
   

print_tic_tac_toe('~', '!')
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39