pepperjack
pepperjack

Reputation: 67

attempting to print multiple lines in a function without generating the 'none' output

I am trying to make a Tic Tac Toe game where the board is printed containing the number pad to make it easier to understand where you are selecting on the game board.

please see the below function and actual example of this output:

def display_board(board):
    print('')
    print(board[7],'|', board[8], '|', board[9])
    print('--|---|--')
    print(board[4],'|', board[5], '|', board[6])
    print('--|---|--')
    print(board[1],'|', board[2], '|', board[3])
    print('')

the idea is to then take the user input (either 'X' or 'O') and throw it up on the board. however I cannot seem to make it work without outputting 'none' at the bottom. is there a way to avoid this for aesthetics?

below is an example with the input being selected and the character being added in its place:

Player 1, please select your game position: 1

7 | 8 | 9
--|---|--
4 | 5 | 6
--|---|--
x | 2 | 3

None

the comments were looking for more...I just did not want to overload the question but here is the rest of it:

def the_meat(first_marker,player1,player2):
    game_board=['#','1','2','3','4','5','6','7','8','9']
    if first_marker ==True: #this means that player 1 is first
        while first_marker is True:
            current_board=display_board(game_board)
            print(current_board)
            position_select=False
            while position_select==False:
                move=input('Player 1, please select your game position: ')
                position_select=free_space(move)
            player_move=int(move)
            game_board[player_move]=player1
            current_board=display_board(game_board)
            print(current_board)


Upvotes: 0

Views: 133

Answers (1)

arthursribeiro
arthursribeiro

Reputation: 336

If you don't put a return statement in your function it will return by default None.

So, if you do: print(display_board(board)) it will execute the print statements you defined in your function and then it will print the returned value of this function (in this case None)


You're calling print function on the variable current_board, according to these lines:

current_board=display_board(game_board)
print(current_board)

That's not necessary, just call the display_board function and you'll have the behavior you're seeking.

So, your function would be:

def the_meat(first_marker,player1,player2):
    game_board=['#','1','2','3','4','5','6','7','8','9']
    if first_marker ==True: #this means that player 1 is first
        while first_marker is True:
            current_board=display_board(game_board)
            print(current_board)
            position_select=False
            while position_select==False:
                move=input('Player 1, please select your game position: ')
                position_select=free_space(move)
            player_move=int(move)
            game_board[player_move]=player1
            display_board(game_board)

Upvotes: 1

Related Questions