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

Python编写Tic Tac Toe井字棋触发IOPub data rate exceeded报错排查

井字棋代码报错问题定位

问题背景

使用列表实现Tic Tac Toe(井字棋)棋盘,已完成棋盘展示函数,回合处理逻辑设计思路为:通过计数器奇偶判断当前玩家回合,用合法索引列表校验输入位置,替换列表对应位置元素填入X或O,配套写了输入校验与错误提示逻辑。运行代码时触发IOPub data rate exceeded报错,原代码与报错信息如下:

原代码:

board= ['','','','','','','','','']
def show_game():
    print(board)
show_game()

def turn_input():
    turn= 0
    acceptable_index = [0,1,2,3,4,5,6,7,8,]
    
    while turn%2==0:
        show_game()
        print('Input your position as an index')
        
        choice_index= 'WRONG'
        while choice_index in acceptable_index== False:
            choice_index=int(input('Player One, you are X and it is your turn, what index do you want to pick? '))
            if choice_index.isdigit()== False or choice_range in acceptable_index== False:
                print("Make sure your index is the interger between 0 and 9")
    turn+=1
        
    while turn%2!=0:
        show_game()
        print('Input your position as an index')
        
        choice_index= "wrong"
        while choice_index in acceptable_index== False :
            choice_index=int(input('Player Two, you are O and it is your turn, what position do you want to pick? '))
            if choice_index.isdigit()==False or choice_index in acceptable_index== False:
                print("Make sure your position is the interger between 0 and 9")
    turn+=1

turn_input()

报错信息:

IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`.

Current values:
NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)
NotebookApp.rate_limit_window=3.0 (secs)

报错根因

这个报错是Jupyter的输出流保护机制,触发原因是代码进入无限死循环,短时间内产生巨量输出超过了传输阈值,和Jupyter配置无关,是代码逻辑存在多处bug导致的:

  • 外层回合循环是死循环:两个while turn%2==0、while turn%2!=0的循环体内没有修改turn变量的逻辑,turn初始值为0,会永远满足第一个循环的判断条件,卡在玩家1回合无限执行。
  • 判断逻辑存在运算符优先级错误:写的choice_index in acceptable_index== False属于Python链式比较,实际执行逻辑为choice_index in acceptable_index and acceptable_index == False,结果永远为False,内层输入校验的循环永远不会退出。
  • 类型判断顺序错误:先把输入通过int()转成了整数类型,后续又对整数调用.isdigit()字符串方法,运行会直接抛出属性错误。
  • 存在未定义变量:玩家1的校验逻辑里写了不存在的变量choice_range,运行会直接抛出名称错误。
  • 核心逻辑缺失:就算校验流程跑通,也没有编写把X/O写入棋盘列表、判断位置是否已被占用的逻辑。

修正后可运行代码

修复了上述所有逻辑bug,同时优化了棋盘展示样式,更符合井字棋的视觉效果:

board = ['','','','','','','','','']
def show_game():
    # 按3*3格式渲染棋盘,空位显示空格
    for i in range(0, 9, 3):
        print(f" {board[i] or ' '} | {board[i+1] or ' '} | {board[i+2] or ' '} ")
        if i < 6:
            print("---+---+---")

def turn_input():
    turn = 0
    acceptable_index = [0,1,2,3,4,5,6,7,8]
    # 棋盘总共9个位置,下满自动结束循环,不会出现死循环
    while turn < 9:
        show_game()
        # 根据回合数判断当前玩家和落子标记
        current_mark = 'X' if turn % 2 == 0 else 'O'
        current_player = 'Player One (X)' if turn % 2 == 0 else 'Player Two (O)'
        input_valid = False
        while not input_valid:
            raw_input = input(f'{current_player}, please input the index (0-8) you want to pick: ')
            # 第一步校验:输入是否为整数
            if not raw_input.isdigit():
                print("Invalid input: please enter an integer between 0 and 8")
                continue
            choice_index = int(raw_input)
            # 第二步校验:索引是否在0-8范围内
            if choice_index not in acceptable_index:
                print("Invalid index: please choose a number between 0 and 8")
                continue
            # 第三步校验:选中位置是否已经被占用
            if board[choice_index] != '':
                print("Invalid choice: this position is already taken, pick another one")
                continue
            # 所有校验通过,落子
            board[choice_index] = current_mark
            input_valid = True
        turn += 1
    # 展示最终棋盘
    show_game()
    print("Game finished!")

turn_input()

注:不需要修改Jupyter的iopub速率限制配置,死循环导致的报错改配置只会让浏览器被巨量输出卡崩,修复循环逻辑即可解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 21:51:44