简单问答电话词典
我想做的是把人的名字和号码放在字典里查找
我想做什么
Enter command (a, f, d, or q).: a
Enter new name................: Perry
Enter new phone number........: 229-449-9683
Enter command (a, f, d, or q).: f
Enter name to look up...:
我想在键入时查找全名和号码
电话字典代码我到目前为止写的:
phone_dict = {}
command = input('Enter command (a, f, d, or q).: ')
newname = input('Enter new name................: ')
newphone = input('Enter new phone number........: ')
while True:
if command == 'a':
newname
newphone
phone_dict[newname] = newphone
print(phone_dict)
# In here, 'while-loop' does not work.
在这里,如果我输入'a'命令并键入名称
这本字典应该是{Perry:229-449-9683}
谢谢,这个问题可能有点困惑,但如果你能帮我解决这个问题,我很高兴
要使用此人的姓或名查找号码,您可以执行以下操作:
在本例中,我使用
query.strip()
删除任何可能导致找不到此人的额外开始/结束空格请让我知道这是否有帮助。谢谢
要从字典中查找结果,您可以循环遍历这些项并检查键是否包含要查找的字符串。如果要获取满足查询的所有值,可以创建另一个列表或字典并存储找到的项:
请注意,这将贯穿字典的所有值:
O(n)
相关问题 更多 >
编程相关推荐