You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Colab中第二个input()输入框不显示的问题求助

Google Colab中第二个input()输入框不显示的问题解决

问题详情

在Google Colab运行Python棋盘游戏代码时,调用input()请求输入出现异常:输入框有时不显示,且总是第二个input()对应的输入框无法加载。尝试给input()直接添加提示语(如input("Where do you want to move the piece to?")),但问题依旧。

相关代码如下:

#--------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

(截图显示:第一次输入完成后,棋盘刷新,"Where to move piece to?..."提示语已输出,但下方无输入框)

原因分析

Colab的输出渲染机制和clear_output()的无参数调用存在冲突:连续调用clear_output()会快速清除旧输出,导致Colab前端来不及正确渲染第二个input()对应的输入组件。

解决方案

方案1:给clear_output()添加wait=True参数

修改两处clear_output()为clear_output(wait=True),这个参数会等待新输出生成后再清除旧内容,避免输入框被意外清除或无法渲染。

修改后的核心代码片段:

#PIECE TO MOVE
#refresh board
clear_output(wait=True)
printboard()

# ... 中间代码省略 ...

#MOVE PIECE
#refresh board
clear_output(wait=True)
printboard()

方案2:用ipywidgets替代原生input()

如果方案1无效,可使用Colab适配的交互式组件获取输入,兼容性更好。示例代码:

from IPython.display import clear_output
import ipywidgets as widgets
from IPython.display import display

# ... 原有棋盘定义和函数保留 ...

# 定义输入组件
piece_input = widgets.Text(description="选子位置:")
move_input = widgets.Text(description="移动到:")
submit_btn = widgets.Button(description="确认")
output = widgets.Output()

def on_submit_clicked(b):
    with output:
        clear_output(wait=True)
        printboard()
        # 处理选子逻辑
        pieceToMove = piece_input.value.strip()
        pieceX = 8 - int(pieceToMove[1])
        pieceY = int(posInAlphabet(pieceToMove[0])) - 1
        pieceType = board[pieceX][pieceY]
        board[pieceX][pieceY] = " □"
        
        clear_output(wait=True)
        printboard()
        # 处理移动逻辑
        movePosition = move_input.value.strip()
        if movePosition == "back":
            board[pieceX][pieceY] = pieceType
            piece_input.value = ""
            move_input.value = ""
            return
        destX = 8 - int(movePosition[1])
        destY = int(posInAlphabet(movePosition[0])) - 1
        board[destX][destY] = pieceType
        piece_input.value = ""
        move_input.value = ""

submit_btn.on_click(on_submit_clicked)
display(piece_input, move_input, submit_btn, output)

方案3:添加短暂延迟(临时 workaround)

在两次输入之间添加time.sleep(0.5),给Colab前端足够的渲染时间:

import time

# ... 原有代码 ...

#MOVE PIECE
#refresh board
clear_output()
printboard()
time.sleep(0.5)  # 添加延迟
print("Where to move piece to? (e.g b2) type 'back' to go back")
movePosition = input()

内容的提问来源于stack exchange,提问作者Mr Pigeon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 10:07:08