我的代码在python shell内运行良好,但是在我的Visual Studio代码上运行它时却不显示任何内容

时间:2019-05-27 12:19:16

标签: python visual-studio

该代码可在Python shell中运行,但不能在VS Code终端中运行。     谁能帮我,我快疯了。

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!")

我需要查看创建的木板,它根本不会在VS代码中显示任何内容

它根本不显示终端内的任何内容,我也没有任何错误。

1 个答案:

答案 0 :(得分:0)

print_board()中定义打印语句时,您从未真正调用它。

只需添加

print_board()

在适当的时候结束。


所以您的代码可能类似于:

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): 

一些注意事项:

  • 如上所示,您可以通过使用VT100 codes上一行(\x1b[1A上一行并删除前一行(\x1b[2K)来替换面板和命令提示符的最后打印重新打印
  • 字符串可以像列表一样倍增(重复)
In [1]: print('a'*3)                                                                      
> aaa
  • 您可以将\n添加到字符串中以换行,而不必多次调用print()
In [25]: print('a\nb')                                                              
> a
> b
  • 您可以使用***来解压缩可迭代变量(列表,元组..)
yourDict = {'a':3, 'b':4}
yourList = [1, 2]

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