首页 > 解决方案 > 显示 EOFerror 的输入语句

问题描述

当我提供如下所述的输入时,我的代码显示输入语句的 EOFError。问题是什么,它返回 EOFError 作为错误。

输入应该像

3, 2, 5 10 2, 10 5 2, 5 15

逗号指下一行

输出应该像

是的,不,不

n=int(input())
t1=0
while n>=t1:
  t=int(input())
  l=[]
  for i in range(t):
      val=int(input())
      l.append(val)
  icream=5
  chefmoney=0
  for i in l:
      if(i==icream):
          chefmoney=chefmoney+icream
      if(i>icream) and (i-icream>chefmoney):
          t=False
      if(i>icream) and (i-icream==chefmoney):
          t=True
  t1=t1+1
if(t==True):
  print("YES")
if(t==False):
  print("NO")

      ```

标签: pythonstringlistloopsinput

解决方案


问题是您阅读的行数多于可用行数。

n=int(input())
t1=0
while n>=t1

应该:

n = int(input())
t1 = 0
while t1 < n:

0比较索引开始时严格小于最大(哨兵)值。为了比较 index <= sentinel 值,您需要从 开始索引1

我还重新安排了您的比较,因为传统上将该比较表达式写为index (comparison) sentinel而不是sentinel (comparison) index.

这是您在软件开发中经常看到的“off by one”错误的一个示例。


推荐阅读