Python文件在Python IDLE中正确运行,但在线提供错误

时间:2018-06-03 10:16:28

标签: python

在每个查询的新行上,如果名称在电话簿中没有相应的条目,请打印Not found,否则以name=phoneNumber格式打印全名和数字。

的Python

n=int(input())

dict={}
for i in range(0,n):
    p=[]
    p.append(input().split())
    dict.update({p[0][0]:int(p[0][1])})

r=[]
while(1):
    z=input()
    if(len(z)!=0):
        r.append(z)
    else:
        break

for l in range(0,len(r)):
    if(r[l] in dict):
        print(r[l]+ "=" + str(dict[r[l]]))
    else:
        print("Not found")

输入

3
a 334234
b 2342342
c 425453
a
b
c
d
e
我的电脑闲置

输出

a=334234
b=2342342
c=425453
Not found
Not found

hackerrank(在线闲置)输出

Traceback (most recent call last):
File "solution.py", line 10, in <module>
z=input()
EOFError: EOF when reading a line

1 个答案:

答案 0 :(得分:0)

不要在built-ins之后命名变量,它们会被遮蔽。通过捕获EOFError

来解决您的问题
# d  is what you called dict - which you should not
d = {}
for n in range(int(input())):
    name,number = [x.strip() for x in input().strip().split()]
    d[name]=number

while True:
    try:
        q = input()
        if q in d:
            print("{}={}".format(q, d[q]))
        else:
            print("Not found")
    except EOFError:
        break