python Hackerrank 中的 EOF 错误

新手上路,请多包涵

试图解决一个问题,但 Hackerrank 的编译器在解析时不断抛出错误 EOFError: don’t know where is mi wrong.

 #!usr/bin/python

b=[]
b=raw_input().split()
c=[]
d=[]
a=raw_input()
c=a.split()
f=b[1]
l=int(b[1])
if(len(c)==int(b[0])):
    for i in range(l,len(c)):
        d.append(c[i])
        #print c[i]
    for i in range(int(f)):
        d.append(c[i])
        #print c[i]
for j in range(len(d)):
    print d[j],

我也试过 try catch 来解决它,但没有得到任何输入。

 try:
    a=input()
    c=a.split()
except(EOFError):
    a=""

输入格式是 2 个间隔的整数,然后是数组

追溯错误是:

 Traceback (most recent call last):
  File "solution.py", line 4, in <module>
    b=raw_input().split()
EOFError: EOF when reading a line

原文由 imshashi17 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 432
2 个回答

我不知道,但提供自定义输入并编译它并让我进入!并通过了所有案例,甚至没有改变任何东西。

原文由 imshashi17 发布,翻译遵循 CC BY-SA 3.0 许可协议

有几种方法可以处理 EOF 错误。

1.抛出异常:

 while True:
  try:
    value = raw_input()
    do_stuff(value) # next line was found
  except (EOFError):
    break #end of file reached

2.检查输入内容:

 while True:
  value = raw_input()
  if (value != ""):
    do_stuff(value) # next line was found
  else:
    break

3. 使用 sys.stdin.readlines() 将它们转换成列表,然后使用 for-each 循环。更详细的解释是 Why does standard input() cause an EOF error

 import sys

# Read input and assemble Phone Book
n = int(input())
phoneBook = {}
for i in range(n):
    contact = input().split(' ')
    phoneBook[contact[0]] = contact[1]

# Process Queries
lines = sys.stdin.readlines()  # convert lines to list
for i in lines:
    name = i.strip()
    if name in phoneBook:
        print(name + '=' + str( phoneBook[name] ))
    else:
        print('Not found')

原文由 Yuchao Jiang 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏