1

On a new line for each query, print Not found if the name has no corresponding entry in the phone book, otherwise print the full name and number in the format 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")

input

3
a 334234
b 2342342
c 425453
a
b
c
d
e

output on my pc idle

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

output on hackerrank (online idle)

Traceback (most recent call last):
File "solution.py", line 10, in <module>
z=input()
EOFError: EOF when reading a line
Borodin
  • 126,100
  • 9
  • 70
  • 144
Raghav
  • 31
  • 3
  • Possible duplicate of [EOFError: EOF when reading a line](https://stackoverflow.com/questions/17675925/eoferror-eof-when-reading-a-line) – Ofer Sadan Jun 03 '18 at 10:25
  • The error is the same for the dupe, the reasons are different - the solutions as well. I dont think the answers there will lead to solving this one, so providing answere here. – Patrick Artner Jun 03 '18 at 10:56

1 Answers1

0

Do not name your variables after built-ins, they get shadowed. Fix your problem by catching the 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
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Sir, i got the upper code correctly(saving into dictionary) but while recalling from dictionary...all the queries are given in one go...and comp. should keep on reading the queries till stop entering(each query in next line) and then it should show me results.In above case it's just showing me result just after a single query. Kindly help me out with this. – Raghav Jun 05 '18 at 05:12
  • @Raghav Your question is 100% from [30-dictionaries-and-maps/problem](https://www.hackerrank.com/challenges/30-dictionaries-and-maps/problem) - this is a solution for this problem. I do not quite get what you mean with upper/lower part. This solution will ask for input while input is given (if finished reading input EOFError is triggered, then it stops). It will print for each input either the _key=value_ (name=telephone) or _Not found_. The solution to your problem is simply catching the error that is created when the last input() tries to caputre something but nothing is given. – Patrick Artner Jun 05 '18 at 05:19