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

CodeChef竞赛Python代码n=int(input())出现EOFError报错求解

CodeChef Python代码输入触发EOFError/NZEC错误解决方法

错误诱因

该错误核心是Python逐行读取输入时,判题机输入流提前结束,常见触发原因如下:

  • 直接使用input()逐行读取,在部分OJ判题环境下存在读取时序问题,第一行就可能读不到有效输入触发EOF
  • 代码中使用split(" ")指定单个空格作为分隔符,若测试用例存在多空格、制表符分隔的情况,会生成空字符串,引发类型转换错误后错位读取输入,最终提前耗尽输入流触发EOF
  • 代码逻辑直接写在全局作用域,受Python解释器行缓冲机制影响,容易出现输入输出异常

优化方案

方案1:优先使用sys.stdin一次性读取所有输入

这是编程竞赛中Python处理输入最稳定的方案,避免逐行读取的EOF问题,修改后的代码示例:

import sys

def main():
    all_input = sys.stdin.read().split()
    idx = 0
    n = int(all_input[idx])
    idx += 1
    for _ in range(n):
        a = int(all_input[idx])
        b = int(all_input[idx+1])
        c = int(all_input[idx+2])
        d = int(all_input[idx+3])
        e = int(all_input[idx+4])
        idx += 5
        if (b + c <= d and a <= e) or (a + c <= d and b <= e) or (a + b <= d and c <= e):
            print("YES")
        else:
            print("NO")

if __name__ == "__main__":
    main()

方案2:最小修改适配

如果不想大改原有代码,可做两处修改即可:

  1. 把input().split(" ")改为input().split(),自动适配任意空白分隔符
  2. 把所有逻辑封装到main()函数中执行,不要直接写在全局域

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 08:36:04