python - 显示 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")
```
解决方案
问题是您阅读的行数多于可用行数。
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”错误的一个示例。
推荐阅读
- php - 为什么在 wordpress 中使用 get_var 获取的值没有立即更新?
- python - 是否可以在 Odoo V13 及更高版本中通过自动操作发送短信?
- c# - XmlException:ASP .NET Core 中缺少根元素
- javascript - 如何在 javascript 中将变量用作函数?
- jquery - Jquery 轻松自动完成样式
- c++ - static_assert in production code header: bad for compilation time?
- php - 读取数组的第一条记录而不是 Laravel 中的第一个字符
- c++ - 从包含结构的 unique_ptr 创建一个 shared_ptr
- python - Pandas Dataframe 根据条件和使用原始值替换字符串值
- javascript - 使用 object.create 创建对象时创建默认参数