contacts = [ {"name": Qi Lee, "mobile": 13946253589}, {"name": Anny Hofman, "mobile": 13651323101} ] 输入示例1: Qi Lee 输出示例1: 13946253589 输入示例2: Yaping Guo 输出示例2: 用户不存在 contacts = [ {"name": "Qi Lee", "mobile": 13946253589}, {"name": "Anny Hofman", "mobile": 13651323101} ] name = input() isFound = for : if : isFound = True print(contact["mobile"]) break if isFound != True: print("用户不存在")
时间: 2023-09-17 08:14:47 浏览: 55
好的,根据你的要求,以下是 Python 代码实现:
```python
# 定义联系人列表
contacts = [
{"name": "Qi Lee", "mobile": 13946253589},
{"name": "Anny Hofman", "mobile": 13651323101}
]
# 获取用户输入的姓名
name = input()
# 查找联系人
isFound = False
for contact in contacts:
if contact["name"] == name:
isFound = True
print(contact["mobile"])
break
# 判断是否找到联系人
if isFound != True:
print("用户不存在")
```
以上代码中,我们首先定义了一个联系人列表 `contacts`,其中每个联系人都是一个字典,包含姓名和手机号两个键值对。接着,我们使用 `input()` 函数获取用户输入的姓名,然后使用 `for` 循环遍历联系人列表,查找是否存在该联系人。如果找到了该联系人,则输出其手机号,并将 `isFound` 标志变量设为 `True`;否则,输出“用户不存在”。
相关问题
在如下通讯录中查找对应的联系人: contacts = [ {"name": Qi Lee, "mobile": 13946253589}, {"name": Anny Hofman, "mobile": 13651323101} ] 输入示例1:
在这个通讯录列表中,你可以通过联系人的名字或手机号码来查找对应的联系人。例如,如果你想要查找名叫Qi Lee的人,你可以这样做:
输入示例1: 如何查找名字为"Qi Lee"的联系人?
输出:你可以使用字典的键值对来查找,代码如下:
```python
contacts = [ {"name": "Qi Lee", "mobile": 13946253589}, {"name": "Anny Hofman", "mobile": 13651323101} ]
target_name = "Qi Lee"
contact_to_find = next((c for c in contacts if c['name'] == target_name), None)
```
如果找到了对应的人,`contact_to_find`将是一个包含`{"name": "Qi Lee", "mobile": 13946253589}`的字典。如果没有找到,`contact_to_find`将为`None`。
相关问题:
1. 如果我想查找手机号码为13946253589的联系人呢?
2. 如果通讯录中有多个名字相同的联系人怎么办?
3. 如果我想添加新的联系人到这个列表,应该怎么做?
在如下通讯录中查找对应的联系人: contacts = [ {"name": Qi Lee, "mobile": 13946253589}, {"name": Anny Hofman, "mobile": 13651323101} ] 输入示例1: Qi Lee 输出示例1: 13946253589 输入示例2: Yaping Guo 输出示例2: 用户不存在 contacts = [ {"name": "Qi Lee", "mobile": 13946253589}, {"name": "Anny Hofman", "mobile": 13651323101} ] name = input() isFound = for : if : isFound = True print(contact["mobile"]) break if isFound != True: print("用户不存在")
好的,我可以为您编写一个查找联系人的程序。请使用以下代码:
```python
contacts = [
{"name": "Qi Lee", "mobile": 13946253589},
{"name": "Anny Hofman", "mobile": 13651323101}
]
name = input()
isFound = False
for contact in contacts:
if contact["name"] == name:
isFound = True
print(contact["mobile"])
break
if isFound != True:
print("用户不存在")
```
请按照要求输入联系人的姓名,程序将输出对应的手机号码。如果联系人不存在,则输出“用户不存在”。