This is a program that creates a phone library with an input giving by the online IDE from Hackerank (python 3), then the code has to make a comparison between the names in the phone book and with a new input of names, and then it prints some statements. The IDE makes some tests. At the beginning it made an EOFError. Then I implemented a new solution, trying to avoid this error and it worked in 2 out of 6 tests. Then, the new error is "Time Limited Exceed"..One of the condition from this exercise is: "After the n lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a to look up, and you must continue reading lines until there is no more input". I paste the code about:
import sys
try:
n = int(input().strip())
phone_list = {}
for i in range(0, n):
a = input().lower().split()
phone_list[f"{a[0]}"] = int(a[1])
friends = list(phone_list.keys())
while input:
name = str(input().strip())
if name in friends:
print(f"{name}={phone_list[name]}")
else:
print("Not found")
except EOFError:
sys.exit()
this is the referent webpage and the task: hackerrank.com/challenges/30-dictionaries-and-maps/problem
The problem is here:
Since the value of
bool(input)is alwaysTrue(you're converting the action functioninputto a boolean value, which is truthy), this loop never ends.You probably wanted something like this in Python >= 3.8:
Or in older versions:
However, note that this is problematic as well:
What if there aren't
nentries? Your script would be waiting for more data to arrive indefinitely. However, I assume that in the specific setup you're in, that would generate anEOFError, but note that this is not generally a good pattern.