0

The code works inside Python shell, but not inside VS Code terminal. Can anyone please help me, I am about to go nuts.

I have tested my code on several ide and it works fine, just on VS 

board = ["  " for i in range(9)]


def print_board():
    row1 = "| {} | {} | {} |".format(board[0], board[1], board[2])
    row2 = "| {} | {} | {} |".format(board[3], board[4], board[5])
    row3 = "| {} | {} | {} |".format(board[6], board[7], board[8])

    print(row1)
    print(row2)
    print(row3)
    print()


def player_move(icon):

    if icon == "X":
        number = 1
    elif icon == "O":
        number = 2

    print("Your turn player {}".format(number))

    choice = int(input("Enter your move (1-9): ").strip())
    if board[choice - 1] == "  ":
        board[choice - 1] = icon
    else:
        print()
        print("That space is taken!")

I need to see the board that I have created, it simply does not show anything inside VS code

It simply does not show anything inside the terminal and I don't get any errors.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
  • Possible duplicate of [What does if \_\_name\_\_ == "\_\_main\_\_": do?](https://stackoverflow.com/questions/419163/what-does-if-name-main-do) – gold_cy May 27 '19 at 12:21

1 Answers1

0

while you define your print statements in print_board() you never actually call it.

Just add

print_board()

to the end and wherever appropriate.


so your code might look something like:

import numpy as np

board = [" " for i in range(9)]
icons = {1:"X", 2:"O"}

def print_board(remove=0):
    if remove > 0:
        print('\x1b[1A\x1b[2K'*remove)
    boardString = "| {} | {} | {} |\n"*3
    print(boardString.format(*board))


def player_move(_turn):
    player = np.mod(_turn,2)+1
    print("Your turn player {}".format(player))

    while True:
        choice = int(input("Enter your move (1-9): "))
        if board[choice - 1] == " ":
            board[choice - 1] = icons[player]
            break
        else:
            print('\x1b[1A\x1b[2K'*3)
            print("That space is taken!")

    print_board(7)

print_board()
for turn in range(9):
    player_move(turn)

| X | O | X |
| O |   |   |
|   |   |   |

Your turn player 1
Enter your move (1-9): 

some notes:

  • As shown above, you can replaces the last print of the board and command prompt by utilising VT100 codes for going up a line (\x1b[1A) and removing a line (\x1b[2K) before printing anew
  • strings can be multiplied (repeated) like lists
In [1]: print('a'*3)                                                                      
> aaa
  • you can add \n for a line break to strings instead of calling print() multiple times
In [25]: print('a\nb')                                                              
> a
> b
  • you can unpack iterable variables (lists, tuples..) by using * or **
yourDict = {'a':3, 'b':4}
yourList = [1, 2]

yourFun(*yourList, **yourDict )
# is equvivalent to:
yourFun(1, 2, a=3, b=4 )

Community
  • 1
  • 1
Argysh
  • 151
  • 4