python - 我在黑客等级引擎中遇到 EOF 错误,而我在本地机器上的代码工作正常
问题描述
我在其上运行 python3 时在 Hacker rank 引擎中遇到此错误。
Traceback (most recent call last):
File "Solution.py", line 8, in <module>
name=input()
EOFError: EOF when reading a line
我的代码是这个
phonebook = {}
total_entries = int(input("Total number of entries: "))
if 1 <= total_entries <= 10 ** 5:
print("Enter names and number separated by space:")
for entries in range(0, total_entries):
items = input("Enter here:")
items = items.split(" ")
phonebook[items[0]] = int(items[1])
queries = []
while True:
queries.append(input("Enter name to be searched:"))
k = queries[-1]
if not k:
break
if 1 <= len(queries) <= 10 ** 5:
for query in queries:
if query == '':
exit(0)
elif query in phonebook.keys():
print(f"{query}={phonebook.get(query)}")
else:
print("Not found")
如果您需要,问题的链接是这样的:问题链接
代码在我的本地机器上运行良好。我不知道为什么它会出现这样的错误。请建议我能做什么!
解决方案
根据 HackerRank 的问题,在 n 行输入之后,会有一些“随机”的输入行。所以你不知道输入何时结束。因此,您会收到“EOF 错误”。一种方法是使用标准输入,如下所示:
from sys import stdin
phone={}
n=int(input())
for i in range(0,n):
name=input()
phone[name]=input()
for i in stdin:
name=input()
re=phone.get(name,"none")
if re!="none":
print("%s=%s"%(name,re))
else:
print("Not found")
不过你的逻辑是错误的。由于输入是在一行中进行的,而您是在两行输入而不是一行
推荐阅读
- c# - 如何选择 C# 列表中的值?
- javascript - 如何使用动画覆盖来掩盖元素?
- r - 同时保存和打印 R 系统调用输出?
- android - 无法从 TextView 中删除 clickListener 并将其设置为可跨文本
- android - 用于 android 数据库的 MVVM 与 SQLiteOpenHelper。有点迷茫
- vba - 为什么在尝试读取记录集字段时未定义访问 vba 抛出子或函数?
- java - ByteBuffer 未填充正确的数据
- javascript - 如何将 setTimeout 函数添加到 vuejs 中的加载程序?(检查示例以获得更好的理解)
- android - 当应用程序在 Google Play 中发布时如何向团队发送电子邮件?
- google-apps-script - Google AppScript Lock 似乎不起作用