![](/img/trans.png)
[英]Why is my Python code not running correctly in Visual Studio Code, but runs fine in IDLE?
[英]My code works fine inside the python shell, but not showing anything when running it on my Visual studio code
代码在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代码中显示任何内容
它根本没有在终端内显示任何内容,我也没有任何错误。
当你在print_board()
定义你的print语句时,你实际上从来没有调用它。
只需添加
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):
一些说明:
\\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 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.