【发布时间】:2015-12-19 08:00:13
【问题描述】:
我在 Python 中有一个问题,我们输入了员工的名字、员工的姓氏和他/她的电话号码。
我们必须创建一个目录,如果我们给出名字、姓氏或两者都给出,则应该生成所有具有共同姓名的员工的编号。
例如员工的名字为John,程序必须打印John Paul和Michel John的电话号码。
d = {}
a = int(input('the total number of employees in the company:'))
for i in range(0, a):
x = input('first name of the employee:')
y = input('the last name of the employee:')
z = int(input('the phone number of the employee:'))
d2 = {x: z, y: z}
d.update(d2)
b = input('the partial or the full name of the employee you need the phone number of:')
print(d[b])
我可以在我的代码中找到错误(当字典更新为通用名称而不是创建一个新条目时,因此在通用条目的情况下只显示一个数字(最后一个条目)。
【问题讨论】:
-
数据结构的方式是问题所在,必须遍历每个键才能找到所需的内容,这违背了 dict 的全部要点,您还不如使用列表。您也不能按照建议使用 in ,因为您最终会得到无意义的输出,因为您将匹配任何子字符串
标签: python python-3.x dictionary