试图解决问题,但是Hackerrank的编译器在解析时不断抛出错误EOFError:不知道哪里出错了。
#!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],
Run Code Online (Sandbox Code Playgroud)
我也尝试尝试抓住解决它,但随后没有输入。
try:
a=input()
c=a.split()
except(EOFError):
a=""
Run Code Online (Sandbox Code Playgroud)
输入格式是2个间隔开的整数,然后是数组
追溯错误是:
Traceback (most recent call last):
File "solution.py", line 4, in <module>
b=raw_input().split()
EOFError: EOF when reading a line
Run Code Online (Sandbox Code Playgroud)
有几种方法可以处理EOF错误。
1.抛出一个异常:
while True:
try:
value = raw_input()
do_stuff(value) # next line was found
except (EOFError):
break #end of file reached
Run Code Online (Sandbox Code Playgroud)
2.检查输入内容:
while True:
value = raw_input()
if (value != ""):
do_stuff(value) # next line was found
else:
break
Run Code Online (Sandbox Code Playgroud)
3.使用sys.stdin.readlines()将它们转换为列表,然后使用for-each循环。更详细的解释是为什么标准input()会导致EOF错误
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')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9479 次 |
| 最近记录: |