当我的代码要求使用
input()
输入时,输入框不会显示在输出框中有时。总是第二个输入不显示,我不确定这是否是一个处理错误以及是否需要一些时间来加载,但下面是我的代码和输出框的屏幕截图。
#--------Chess--------#
#import
from IPython.display import clear_output
#chessboard
board = [["♜", "♞", "♝", "♛", "♚", "♝", "♞", "♜"],
["♟", "♟", "♟", "♟", "♟", "♟", "♟", "♟"],
["□ ", "□ ", "□ ", "□ ", "□ ", "□ ", "□ ", "□"],
["□ ", "□ ", "□ ", "□ ", "□ ", "□ ", "□ ", "□"],
["□ ", "□ ", "□ ", "□ ", "□ ", "□ ", "□ ", "□"],
["□ ", "□ ", "□ ", "□ ", "□ ", "□ ", "□ ", "□"],
["♙", "♙", "♙", "♙", "♙", "♙", "♙", "♙"],
["♖", "♘", "♗", "♕", "♔", "♗", "♘", "♖"]]
#other variables
gameEnded = False
#functions
def printRank(rank):
rank -= 1
print("| |" + board[rank][0] + " " + board[rank][1] + " " + board[rank][2] + " " + board[rank][3] + " " + board[rank][4] + " " + board[rank][5] + " " + board[rank][6] + " " + board[rank][7] + "| |")
def printboard():
printRank(1)
printRank(2)
printRank(3)
printRank(4)
printRank(5)
printRank(6)
printRank(7)
printRank(8)
def posInAlphabet(letter):
pos = ord(letter) - 96
return pos
#gameLoop
while gameEnded == False:
#PIECE TO MOVE
#refresh board
clear_output()
printboard()
#take input for piece to move
print("Which piece to move? (e.g a1)")
pieceToMove = input()
#get actual position of that piece
pieceX = 8 - int(pieceToMove[1])
pieceY = int(posInAlphabet(pieceToMove[0])) - 1
print(pieceX)
print(pieceY)
#get the type of that piece
pieceType = board[pieceX][pieceY]
#replace that piece with a blank space
board[pieceX][pieceY] = " □"
#MOVE PIECE
#refresh board
clear_output()
printboard()
#take input for plate to move the piece to
print("Where to move piece to? (e.g b2) type 'back' to go back")
movePosition = input() #THIS IS THE LINE THE ERROR OCCURS IN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if movePosition == "back":
board[pieceX][pieceY] = pieceType
continue
#get actual position to move the piece to
destX = 8 - int(movePosition[1])
destY = int(posInAlphabet(movePosition[0])) - 1
#replace that space with the piece to move
board[destX][destY] = pieceType
我尝试添加提示,如下所示:
input("Where do you want to move the piece to?")
因为这似乎对网上其他一些人有用,但对我来说没有任何改变。
我也尝试做类似的事情,想要清除输出以显示新板,但是,由于某种原因,使用clear_output()导致输入框有时不显示。
相反,如果您使用:
from google.colab import output
output.clear() # where you need to clear output
输入框不消失。我希望这有帮助。