Hackerrank第8天:字典和地图问题(使用Python)

Hackerrank第8天:字典和地图问题(使用Python),python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,目标 今天,我们将学习使用映射或字典数据结构的键值对映射。查看教程选项卡以获取学习材料和教学视频 任务 给定姓名和电话号码,组装一个电话簿,将朋友的姓名映射到他们各自的电话号码。然后,您将获得一个未知数量的姓名,以查询您的电话簿。对于每个查询的项目,以name=phoneNumber的形式在新行上打印电话簿中的相关条目;如果未找到的条目,请改为“未找到打印” 注意:您的电话簿应该是字典/Map/HashMap数据结构。 输入格式 第一行包含一个整数,表示通讯录中的条目数。 随后的每一行以空格分隔

目标
今天,我们将学习使用映射或字典数据结构的键值对映射。查看教程选项卡以获取学习材料和教学视频

任务
给定姓名和电话号码,组装一个电话簿,将朋友的姓名映射到他们各自的电话号码。然后,您将获得一个未知数量的姓名,以查询您的电话簿。对于每个查询的项目,以name=phoneNumber的形式在新行上打印电话簿中的相关条目;如果未找到的条目,请改为“未找到打印”

注意:您的电话簿应该是字典/Map/HashMap数据结构。

输入格式
第一行包含一个整数,表示通讯录中的条目数。 随后的每一行以空格分隔的值的形式在一行上描述一个条目。第一个值是一个朋友的姓名,第二个值是一个数字的电话号码。
在电话簿条目行之后,有一个未知的查询行数。每一行(查询)都包含一个要查找的行,您必须继续阅读这些行,直到没有更多的输入

注意:姓名由小写英文字母组成,仅为名字。

输出格式
在每个查询的新行上,如果姓名在电话簿中没有相应条目,则打印“未找到”;否则,以name=phoneNumber的格式打印完整的和

样本输入

3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry
样本输出

sam=99912222
Not found
harry=12299933
我的代码:

# No. of dictionary inputs
n = int(input())

# Code to fill the phonebook with entries
phonebook = {}
for _ in range(1, n+1):
  entry = input().split(" ")
    
  phonebook[entry[0]] = entry[1]

# Code for query intake
queries = []
while(1):
  queries.append(input())

  #need to figure out when the user stops input and need to break this loop then
  if (input() == ""):
    break

# Code for query result
for i in range(0, len(queries)):
  if(queries[i] in phonebook):
    print(queries[i] + "=" + phonebook[queries[i]])
    # print(f"{queries[i]}={phonebook[queries[i]]}")
  else:
    print("Not found")
# Code to fill the phonebook with entries
phonebook = dict() #Declare a dictionary
for _ in range(int(input())):
    key, value = input().split()
    
    phonebook[key] = value

#Trick - If there is no more input stop the program
try:
    # Code for query intake
    queries = []
    while True:
        line = input()
        if line == "":
            break
        queries.append(line)
except Exception:
    pass

# Code for query result
for i in range(0, len(queries)):
    if(queries[i] in phonebook):
        print(queries[i] + "=" + phonebook[queries[i]])
        # print(f"{queries[i]}={phonebook[queries[i]]}")
    else:
        print("Not found")
我面临的问题: 当我运行代码时,我输入样本,直到最后一切都正常运行,但是,在打印结果时,查询“edward”没有得到输出。 “edward”所需的输出将为“Not Found”,但是,每个偶数输入都会丢失,这可能是由于while循环中的if语句

应该只使用一次
input()
,然后使用
append()
break

while True:
  line = input()
  if line == "":
    break
  queries.append(line)

我的最后一段代码运行正常,没有EOF错误,因为我使用try-except-block:

# No. of dictionary inputs
n = int(input())

# Code to fill the phonebook with entries
phonebook = {}
for _ in range(1, n+1):
  entry = input().split(" ")
    
  phonebook[entry[0]] = entry[1]

# Code for query intake
queries = []
while(1):
  queries.append(input())

  #need to figure out when the user stops input and need to break this loop then
  if (input() == ""):
    break

# Code for query result
for i in range(0, len(queries)):
  if(queries[i] in phonebook):
    print(queries[i] + "=" + phonebook[queries[i]])
    # print(f"{queries[i]}={phonebook[queries[i]]}")
  else:
    print("Not found")
# Code to fill the phonebook with entries
phonebook = dict() #Declare a dictionary
for _ in range(int(input())):
    key, value = input().split()
    
    phonebook[key] = value

#Trick - If there is no more input stop the program
try:
    # Code for query intake
    queries = []
    while True:
        line = input()
        if line == "":
            break
        queries.append(line)
except Exception:
    pass

# Code for query result
for i in range(0, len(queries)):
    if(queries[i] in phonebook):
        print(queries[i] + "=" + phonebook[queries[i]])
        # print(f"{queries[i]}={phonebook[queries[i]]}")
    else:
        print("Not found")

如果您的问题是针对Python2的,请不要使用标记。如果它是特定于Python3的,请不要使用该标记(或者在标题中建议这是Python2的问题)。如果这两个标签都不是特定的,请不要使用这两个标签中的任何一个。您可以在中阅读有关如何标记Python问题的更多信息。