I have managed to create a custom sized grid which prints correctly but when I try and print X or 0 a new line is started. Please see the "test" result below.

def createBoard():
    global rows,columns,board
    rows = input("How many rows (more than 4)? ")
    columns = input("How many columns (more than 4)? ")
    
    board = {}
    min = 4
    if rows < min:
        createboard()
    elif columns < min:
        createboard()
    for x in range(rows):
        for y in range(columns):
            board[(x,y)] = "-"

def printBoard():
    for x in range(rows):
        print "|",
        for y in range(columns):
            if board[(x,y)] == "-":
                print "- |",
            else:
                print board[(x,y)], "|"
        print ""

def test():
    print
    print ">>Test<<"
    board[0,0] = "X"
    board[0,1] = "O"
    printBoard()

createBoard()
printBoard()
test()

Is there anyway to fix this so it prints like;
| X | O | - | - |

Recommended Answers

All 2 Replies

A new line is printed when you use "print" by itself.

# The below will actually display "Alpha\n"
print "Alpha"

# You can place a comma after the statement to not send the newline, but it will add a space
# The below will display: "Alpha Beta Gamma"
print "Alpha", "Beta", "Gamma"

# If you use string concatenation (plus sign) you can stop the space and the newline.
# The snippet below displays: "AlphaBetaGamma"
print "Alpha" + "Beta" + "Gamma"

That is *about* it. Hope it helps :)

Thanks I was missing a comma, works now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.